Retrofit简单介绍和使用
2016-02-22 22:09
351 查看
原文地址:
https://futurestud.io/blog/retrofit-getting-started-and-android-client#
对于后面的具体版本,一般使用官方最新的版本。
Retrofit 2默认情况下利用okhttp为网络层,在它的基础上建立的。你不需要显式地定义okhttp作为你的项目的依赖性,除非你有一个特定的版本要求。
ServiceGenerator 类使用 Retrofit的restadapter Builder与给定的API库的URL创建一个新的REST客户端。例如,GitHub的API库的网址是https://api.github.com/。参数serviceclass是定义的API请求注释的类或接口。下面显示的具体用法的 Retrofit以及如何定义一个实例的客户端。
现在,你可以在Retrofit’s builder上调用:
使GSON成为Retrofit内部默认的JSON转化器。
{owner} 和 {repo} 在调用时会被替换成传入的参数,这个方法返回的是
我们想要得到的Contributor集合
Contributor 实体类:
在上面添加JSON转化器后,Retrofit 会自动把请求的数据转化成实体类。
最后请求的代码:
首先通过ServiceGenerator 实例化 GitHubClient ,然后调用 GitHubClient 实例的contributors方法,得到Contributor集合,至此,一次完整的网络请求已经完成。
Demo地址:
http://download.csdn.net/detail/jiangxuqaz/9440960
https://futurestud.io/blog/retrofit-getting-started-and-android-client#
Retrofit是什么
对于Retrofit ,官方的描述是:“A type-safe REST client for Android and Java.”它可以通过注解来描述Http请求,URL参数,查询参数,同时,它还支持多个请求体和文件上传。引入到Android工程
dependencies { // Retrofit & OkHttp compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' }
对于后面的具体版本,一般使用官方最新的版本。
Retrofit 2默认情况下利用okhttp为网络层,在它的基础上建立的。你不需要显式地定义okhttp作为你的项目的依赖性,除非你有一个特定的版本要求。
创建ServiceGenerator
首先是需要创建一个ServiceGenerator 类,它是使用整个框架的核心,它只有一个方法用来创建REST adapterpublic 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); } }
ServiceGenerator 类使用 Retrofit的restadapter Builder与给定的API库的URL创建一个新的REST客户端。例如,GitHub的API库的网址是https://api.github.com/。参数serviceclass是定义的API请求注释的类或接口。下面显示的具体用法的 Retrofit以及如何定义一个实例的客户端。
JSON映射
使用Retrofit 2时,需要明确的为Retrofit实例添加一个转化器,在上面的build.gradle文件中,为了引入GSON转化器,我们添加了下面这样一行:compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
现在,你可以在Retrofit’s builder上调用:
.addConverterFactory(GsonConverterFactory.create())
使GSON成为Retrofit内部默认的JSON转化器。
Retrofit 请求网络
下面是一个例子,客户端通过Retrofit 从github上面请求数据。首先新建一个GitHubClient 类,定义一个从github上获取一个开源库所有的贡献者的方法:public interface GitHubClient { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors( @Path("owner") String owner, @Path("repo") String repo ); }
{owner} 和 {repo} 在调用时会被替换成传入的参数,这个方法返回的是
我们想要得到的Contributor集合
Contributor 实体类:
public class Contributor { String login; int contributions; }
在上面添加JSON转化器后,Retrofit 会自动把请求的数据转化成实体类。
最后请求的代码:
// 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 + ")"); }
首先通过ServiceGenerator 实例化 GitHubClient ,然后调用 GitHubClient 实例的contributors方法,得到Contributor集合,至此,一次完整的网络请求已经完成。
Demo地址:
http://download.csdn.net/detail/jiangxuqaz/9440960
相关文章推荐
- 跟我一起看Retrofit 2.0的源码
- http缓存
- Retrofit之Query注解
- Retrofit之Query注解
- Retrofit 入门--part2
- #Retrofit 入门--Part 1
- 解决找不到okio.buffer.writeUTF8CodePoint 的方法
- Retrofit发送网络请求和解析json实例
- Retrofit2(2.x)帮助类(全套)
- 深入解析开源项目之Retrofit(二)框架篇
- 深入解析开源项目之Retrofit(一)使用篇
- Retrofit框架使用笔记
- retrofit2.0.0-beta4 初步使用.
- Android 中 Retrofit 结合 RxJava使用
- Retrofit 源码解读之离线缓存策略的实现
- 使用Retrofit搭建自己的网络请求框架
- Retrofit 和 RxJava 结合使用
- Retrofit基础入门(1)
- Retrofit — 实现Basic Authentication(2)
- Android 网络开源库-Retrofit(一)简单介绍