前言
Retrofit現在真的是炒雞火,前不久用了之后,并且與rxjava配合得簡直天衣無縫,我便深深的愛上了這個框架。
概述
Retrofit是Square公司開發,針對Android網絡請求的框架。通過文檔http://square.github.io/retrofit/可以看到它的介紹
A type-safe HTTP client for Android and Java
簡單的說它就是一個HTTP請求工具,這里提到了類型安全,而Retrofit就是通過聲明泛型來實現類型安全的,也就是說通過聲明泛型能夠約束最終我們想要的對象是什么。
用法
Retrofit 可以利用接口,方法和注解參數(parameter annotations)來聲明式定義一個請求應該如何被創建。這里酷炫的注解參數也是我深深喜愛這個框架的原因之一。比如我們需要請求以下的API
https://api.github.com/repos/{owner}/{repo}/contributors
查看GitHub上"owner"的"repo"中的contributors,首先要創建一個以下的接口:
public interface GitHubService {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> repoContributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
然后通過Retrofit生成一個此接口的實現類:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
默認的,Retrofit只能接收OkHttp'sRequestBody
并轉換成OkHttp'sResponseBody
,不過在這里在創建Retrofit對象時,可以根據需要加入Converters,同時還需要在build.gradle
中添加依賴。
- Gson: com.squareup.retrofit2:converter-gson
- Jackson: com.squareup.retrofit2:converter-jackson
- Moshi: com.squareup.retrofit2:converter-moshi
- Protobuf: com.squareup.retrofit2:converter-protobuf
- Wire: com.squareup.retrofit2:converter-wire
- Simple XML: com.squareup.retrofit2:converter-simplexml
- Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
這里獲取的是json數據,那么修改代碼成:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
接著獲取接口中Call
的實例:
Call<List<Contributor>> call = service.repoContributors("square", "retrofit");
最后就可以通過執行call
獲取到數據了:
call.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Response<List<Contributor>> response) {
for (Contributor contributor : response.body()) {
System.out.println(contributor.login + ":" + contributor.contributions );
}
}
@Override
public void onFailure(Throwable t) {
}
});
這里的Contributor
就是我們創建的數據bean類,在此例子的api中,我們可以獲取到很多數據,但是我們只需要篩選自己需要的數據創建一個bean類傳入就可以了。
public class Contributor{
String login;
int contributions;
}
支持rxjava
當然,Retrofit還支持rxjava,這時我們需要在創建Retrofit實例的時候再加入一個adapter:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
之前的接口也需要修改,這時候我們需要返回的不再是Call
,而是Observable
public interface GitHubService {
@GET("/repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> repoContributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
獲取到Observable實例之后就是一連串的rxjava的代碼,比如子線程生產數據,主線程消費數據,更新UI等等。
service.repoContributors("square", "retrofit")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Contributor>>() {
@Override
public void onStart() {
showProgressBar();
}
@Override
public void onCompleted() {
dismissProgressBar();
}
@Override
public void onError(Throwable e) {
Log.i(">>>", "onError " + e.toString());
}
@Override
public void onNext(List<Contributor> list) {
for (Contributor contributor : list) {
System.out.println(contributor.login + ":" + contributor.contributions );
}
}
});
注解詞
前面說到了Retrofit可以通過注解參數的方式來聲明定義一個請求,一下就來介紹其中重要的注解詞用法。
GET請求
-
@Query
Get方法請求參數都會以key=value的方式拼接在url后面。
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.douban.com/v2/") .addConverterFactory(GsonConverterFactory.create()) .build(); Call<BookBean> call = retrofit.create(DemoService.class).queryDemo(1); String url = call.request().url().toString(); Log.i(">>>", "onCreate :" + url);
打印結果:</br>
https://api.douban.com/list?page=1
-
@QueryMap
如果拼接在后面的參數過多,可以通過
QueryMap
方式將所有的參數集成在一個Map集合中傳遞。Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.douban.com/v2/") .addConverterFactory(GsonConverterFactory.create()) .build(); Map<String, String> options = new HashMap<>(); options.put("start", "0"); options.put("count", "3"); Call<BookBean> call = retrofit.create(DemoService.class).queryMapDemo(options); String url = call.request().url().toString(); Log.i(">>>", "onCreate :" + url);
打印結果:</br>
https://api.douban.com/v2/book/search?count=3&start=0
-
@Path
在url中的參數需要請求方提供的時候,可以利用path。
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.douban.com/v2/") .addConverterFactory(GsonConverterFactory.create()) .build(); Call<BookBean> call = retrofit.create(DemoService.class).pathDemo(1); String url = call.request().url().toString(); Log.i(">>>", "onCreate :" + url);
打印結果:</br>
https://api.douban.com/image/1
POST請求
-
@Field
POST請求需要把參數放置在請求體中,而不是拼接在url后面。
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.douban.com/v2/") .addConverterFactory(GsonConverterFactory.create()) .build(); Call<BookBean> call = retrofit.create(DemoService.class).fieldDemo("zhangsan", "businessman");
- 這里的注解還需要一個@FormUrlEncoded,將會自動將請求參數的類型調整為application/x-www-form-urlencoded。
-
@FieldMap
如果有更多的請求參數的時候,通過一個一個的參數傳遞就顯得很麻煩而且容易出錯,那么這個時候就可以用FieldMap
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.douban.com/v2/") .addConverterFactory(GsonConverterFactory.create()) .build(); Map<String, String> fields = new HashMap<>(); fields.put("start", "0"); fields.put("count", "3"); Call<BookBean> call = retrofit.create(DemoService.class).fieldMapDemo(fields);
Demo GitHub:https://github.com/cgzysan/RetrofitDemo</br>
同時我也用Retrofit寫了一個Gnak.io:https://github.com/cgzysan/Gank.io</br>
Retrofit未完待續!