網(wǎng)絡(luò)請(qǐng)求庫(kù)有很多優(yōu)秀的開(kāi)源項(xiàng)目okhttp,volley都是很不錯(cuò)的,但是個(gè)人比較喜歡Retrofit。原因有以下幾點(diǎn):
1.一個(gè)類(lèi)型安全的REST客戶(hù)端 。
2.通過(guò)GsonConverter可以直接把服務(wù)器響應(yīng)的json字符串映射成對(duì)象,這一切都是自動(dòng)化的。當(dāng)然還有其他的轉(zhuǎn)換器并且支持自定義非常的靈活。
3.支持同步請(qǐng)求和異步請(qǐng)求
4.2.0開(kāi)始加入對(duì)Rxjava的支持,配合Rxjava編程爽爆了,代碼變的很清晰。
5.2.0開(kāi)始可以很輕松的取消請(qǐng)求,你只需調(diào)用call.cancel()
6.性能和速度都比volley等更好。
重要的事情強(qiáng)調(diào)一下本文針對(duì)的是Retrofit 2.0講的,Retrofit 1.xx的版本和2.0版本有許多改動(dòng)。

步驟:
1.首先導(dǎo)入需要的庫(kù)
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
2.新建一個(gè)interface接口,用于存放和服務(wù)器交互的接口,針對(duì)常用操作下面各寫(xiě)一個(gè)范例
- Get無(wú)參數(shù)請(qǐng)求
public interface MeizhiApi { @GET("data/福利/10/{day}")//()里面的是相對(duì)路徑,當(dāng)然絕對(duì)路徑也是可以的 public Call<BeanMezhi> getMeizhi(@Path("day") int day);//{}里面的是要替換的內(nèi)容 用注解@Path映射
}
- Get單個(gè)參數(shù)請(qǐng)求
```java
public interface SmsApi {
@GET("http://xxx/getSmsCode")
public Call<JSONObject> getSms(@Query("tel") String tel);//相當(dāng)于http://xxx/getSmsCode?tel="xx"
}
- Get多個(gè)參數(shù)請(qǐng)求
public interface SmsApi { @GET("http://weixing.wxspider.com:8087/appVoip!getSmsCode") public Call<JSONObject> getSms(@QueryMap Map<String, String> options);
}
- Post請(qǐng)求
```java
public interface DemoApi {
@POST("checkupdate")
public Call<BeanVersion> getVersion(@Body HashMap type);//注意這里用的是@Body
}
- 表單請(qǐng)求
public interface DemoApi {
@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);
}
- 文件上傳
```java
public interface DemoApi {
@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
}
注意:上面所有的接口返回的都是Call類(lèi)型的這是和1.x版本不同的
3.實(shí)例化上面的接口
public class ApiServiceManager {
private static ApiServiceManager ourInstance = new ApiServiceManager();
public static ApiServiceManager getInstance() {
return ourInstance;
}
private DemoApi demoApi;
private MeizhiApi meizhiApi;
private SmsApi smsApi;
private ApiServiceManager() {
Retrofit.Builder builderWithGson = new Retrofit.Builder()
.baseUrl(Config.DemoBaseUrl)//設(shè)置基礎(chǔ)url
.addConverterFactory(GsonConverterFactory.create());//Gson轉(zhuǎn)換器直接返回對(duì)象
Retrofit retrofit = builderWithGson.build();
demoApi = retrofit.create(DemoApi.class);//拿到和服務(wù)器交互的接口實(shí)例
builderWithGson.baseUrl(Config.MeizhiBaseUrl);
retrofit = builderWithGson.build();
meizhiApi = retrofit.create(MeizhiApi.class);
Retrofit.Builder builderWithJson = new Retrofit.Builder()
.addConverterFactory(JsonConverterFactory.create());//Json轉(zhuǎn)換器返回JSONObject,因?yàn)橛行┙涌诜祷氐臄?shù)據(jù)很簡(jiǎn)單不想寫(xiě)個(gè)Bean
retrofit = builderWithJson.build();
smsApi = retrofit.create(SmsApi.class);
}
public DemoApi getDemoApi() {
return demoApi;
}
public MeizhiApi getMeizhiApi() {
return meizhiApi;
}
public SmsApi getSmsApi() {
return smsApi;
}
}
4.使用接口
- 同步請(qǐng)求使用方式
new Thread(new Runnable() { @Override public void run() { Call<BeanMezhi> mezhiCall = ApiServiceManager.getInstance().getMeizhiApi().getMeizhi(1); Response<BeanMezhi> response = null;//同步請(qǐng)求會(huì)阻塞線程,因此你不能在安卓的主線程中調(diào)用,不然會(huì)面臨NetworkOnMainThreadException,想調(diào)用execute方法,請(qǐng)?jiān)诤笈_(tái)線程執(zhí)行 try { response = mezhiCall.execute(); if (response.body() != null) {//如果不能解析成對(duì)應(yīng)的實(shí)體BeanMezhi則response.body()的值是空 Log.d(tag, new Gson().toJson(response.body()).toString()); } } catch (IOException e) { e.printStackTrace(); } } }).start(); - 異步請(qǐng)求使用方式
HashMap map = new HashMap(); map.put("ostype", "android"); Call<BeanVersion> versionCall = ApiServiceManager.getInstance().getDemoApi().getVersion(map); versionCall.enqueue(new Callback<BeanVersion>() {//異步請(qǐng)求 @Override public void onResponse(Call<BeanVersion> call, Response<BeanVersion> response) {//回調(diào)運(yùn)行在主線程 if (response.body() != null) { Log.d(tag, new Gson().toJson(response.body()).toString()); } } @Override public void onFailure(Call<BeanVersion> call, Throwable t) { Log.d(tag, "onFail"); } });
5.取消正在進(jìn)行中的業(yè)務(wù)
call.cancel();
```
基本使用介紹完了,如果需要自定義Converter或者自定義CallAdapter,那么請(qǐng)繼續(xù)往下看。
---
**1. Converter** 官方提供的轉(zhuǎn)換器有
Gson: com.squareup.retrofit:converter-gson
Jackson: com.squareup.retrofit:converter-jackson
Moshi: com.squareup.retrofit:converter-moshi
Protobuf: com.squareup.retrofit:converter-protobuf
Wire: com.squareup.retrofit:converter-wire
Simple XML: com.squareup.retrofit:converter-simplexml
如何自定義轉(zhuǎn)換器?
你也可以通過(guò)實(shí)現(xiàn)Converter.Factory接口來(lái)創(chuàng)建一個(gè)自定義的converter。以JsonConverter示例:
```java
public class JsonConverterFactory extends Converter.Factory {
public static JsonConverterFactory create() {
return new JsonConverterFactory();
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new JsonResponseBodyConverter<JSONObject>();
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new JsonRequestBodyConverter<JSONObject>();
}
}
final class JsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
JsonRequestBodyConverter() {
}
public RequestBody convert(T value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value.toString());
}
}
final class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
JsonResponseBodyConverter() {
}
@Override
public T convert(ResponseBody value) throws IOException {
JSONObject jsonObj;
try {
jsonObj = new JSONObject(value.string());
return (T) jsonObj;
} catch(JSONException e) {
return null;
}
}
}
2. CallAdapter
在interface接口定義中,retrofit2.0默認(rèn)都是返回的Call<T>模式的,如果我們想返回其他的類(lèi)型也是可以,retrofit已經(jīng)為Rxjava粉絲們準(zhǔn)備了CallAdapter,它將作為Observable返回。使用它必須引入以下兩個(gè)庫(kù)
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
并在Retrofit Builder鏈表中調(diào)用addCallAdapterFactory
Retrofit.Builder builderWithGson = new Retrofit.Builder()
.baseUrl(Config.DemoBaseUrl)//設(shè)置基礎(chǔ)url
.addConverterFactory(GsonConverterFactory.create())//Gson轉(zhuǎn)換器直接返回對(duì)象
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());//增加RxjavaCallAdapter
Retrofit retrofit = builderWithGson.build();
接下來(lái)看怎么結(jié)合Rxjava調(diào)用接口
//定義接口
@GET("data/福利/10/{day}")
public Observable<BeanMezhi> getMeizhi2(@Path("day") int day);
//調(diào)用接口
Observable<BeanMezhi> observable = ApiServiceManager.getInstance().getMeizhiApi().getMeizhi2(1);
observable.subscribeOn(Schedulers.io())//獲取數(shù)據(jù)指定運(yùn)行在io線程
.observeOn(AndroidSchedulers.mainThread())//發(fā)布到android主線程
.subscribe(new Action1<BeanMezhi>() {
@Override
public void call(BeanMezhi beanMezhi) {
Log.d(tag, new Gson().toJson(beanMezhi).toString());//處理數(shù)據(jù)這里已經(jīng)是運(yùn)行在主線程了
}
});
在使用rxjava以后我們不在需要寫(xiě)new Thread().start 這些“臟”代碼了,rxjava對(duì)線程的調(diào)度非常強(qiáng)大,那么有同學(xué)會(huì)說(shuō)感覺(jué)還不如返回Call<T>模式的然后異步調(diào)用來(lái)的簡(jiǎn)單。好吧,如果要對(duì)返回的數(shù)據(jù)先過(guò)濾,在排序,還要存儲(chǔ)數(shù)據(jù)庫(kù)等等一系列處理,你該怎么辦?有了rxjava處理這種復(fù)雜的數(shù)據(jù)流一切就會(huì)變的簡(jiǎn)單清晰,如果在配合lambda表達(dá)式可以說(shuō)是如虎添翼。
項(xiàng)目主頁(yè): http://square.github.io/retrofit/
JSONCoverter:https://github.com/brokge/Retrofit2.0-JSONCoverter