燥热的retrofit(一)
来源:互联网 发布:融合app软件 编辑:程序博客网 时间:2024/04/28 04:10
说来惭愧,retrofit那么热,so火,我居然最近才去研究,不过好歹写了一个项目了,也算是对retrofit有一定的了解吧,那么现在讲讲啦,嘿嘿。
首先项目中加retrofit就得这么搞。
dependencies { // Retrofit & OkHttp compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'}
前期准备工作:
public class ServiceGenerator { public static final String API_BASE_URL = "http://your.api-base.url"; private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S> serviceClass) { Retrofit retrofit = builder.client(httpClient.build()).build(); return retrofit.create(serviceClass); }}
public interface GitHubClient { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors( @Path("owner") String owner, @Path("repo") String repo );}static class Contributor { String login; int contributions;}
用法如下:
public static void main(String... args) { // Create a very simple REST adapter which points the GitHub API endpoint. GitHubClient client = ServiceGenerator.createService(GitHubClient.class); // Fetch and print a list of the contributors to this library. Call<List<Contributor>> call = client.contributors("fs_opensource", "android-boilerplate"); try { List<Contributor> contributors = call.execute().body(); } catch (IOException e) { // handle errors } for (Contributor contributor : contributors) { System.out.println( contributor.login + " (" + contributor.contributions + ")"); }}
本篇是翻译国外网站的,觉得人已经写的很好了,就没必要自己再写了。
参考网站:https://futurestud.io/blog/retrofit-getting-started-and-android-client
0 0
- 燥热的retrofit(一)
- 燥热人生的无聊瞎想
- 2014,说说这个燥热的暑假
- Retrofit的进阶之路(一)
- Retrofit的使用详解(一)
- Retrofit的简单使用(一)
- Retrofit的使用【一】
- retrofit实现(一)
- Retrofit的使用教程(一)
- retrofit的实现原理(一)
- Android Retrofit使用(一)
- Retrofit框架学习(一)
- Retrofit & OkHttp系列(一)
- Retrofit用法初识(一)
- Android retrofit历程(一)
- Retrofit 学习记录(一)
- android retrofit 示例(一)
- Retrofit源码解析(一)
- 微风轻抚月,此去一身空
- Android异步处理系列文章索引
- 选择P2P平台的技巧和方法
- vmware安装ubuntu详细步骤
- 003——struts文件下载功能
- 燥热的retrofit(一)
- UI_UIStepper
- android之.9.png图片制作
- MSP430程序升级方式探讨
- 对于Android开发环境的搭建
- Mybatis使用工具生成相应的代码
- 判断数值型的字符串方法小结
- CADisplayLink
- Perf -- Linux下的系统性能调优神器