PS:年後一直沒有更新博客,實在是太忙了。今天周六也是終於抽出了時間,就決定寫一篇Retrofit結合RxJava使用說明。剛好現在寫的的項目中也有用到,趁熱打鐵簡單說說。最近也不知道博客寫什麼內容,大家有什麼想了解的內容,也可以評論告訴我,我盡力解答。
如果對Retrofit不了解可以看我之前的一篇博客:Retrofit 2.0使用 ,這次的內容也是在此基礎上的。關於RxJava可以參考:學習RxJava(資料匯總),這裡我就不多說了。
我們需要添加相應的依賴:
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
首先我在網上隨便找了一個接口用於測試:http://apistore.baidu.com/microservice/cityinfo?,Get請求,參數是cityname
也就是你要查詢的城市名稱。我以查詢西安為例。
我們首先直接請求看一下返回結果:
{
"errNum": 0,
"retMsg": "success",
"retData": {
"cityName": "西安",
"provinceName": "陝西",
"cityCode": "101110101",
"zipCode": "710000",
"telAreaCode": "029"
}
}
那麼我們先寫一個City對象用於Gson解析(這裡我們只獲取retData中的數據,後面我會用自定義轉換器轉換):
public class City {
private String cityName;
private String provinceName;
private String cityCode;
private String zipCode;
private String telAreaCode;
public City(String cityName, String provinceName, String cityCode, String zipCode, String telAreaCode) {
this.cityName = cityName;
this.provinceName = provinceName;
this.cityCode = cityCode;
this.zipCode = zipCode;
this.telAreaCode = telAreaCode;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getTelAreaCode() {
return telAreaCode;
}
public void setTelAreaCode(String telAreaCode) {
this.telAreaCode = telAreaCode;
}
}
Api請求接口:
public interface CityApi {
@GET("cityinfo?")
Observable getCityInfo(@Query("cityname") String cityname);
}
自定義轉換器:
public class DeserializerCity implements JsonDeserializer {
@Override
public T deserialize(JsonElement je, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
// 轉換Json的數據
JsonElement results = je.getAsJsonObject().get("retData");
return new Gson().fromJson(results, typeOfT);
}
}
創建CityApi類:
public class CityService {
public static final String CITY_URL = "http://apistore.baidu.com/microservice/";
private static Gson customGsonInstance = new GsonBuilder()
.registerTypeAdapter(City.class,new DeserializerCity())
.create();
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(CITY_URL)
.addConverterFactory(GsonConverterFactory.create(customGsonInstance))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
public static CityApi createCityService() {
return retrofit.create(CityApi.class);
}
}
調用:
public void getCityInfo(String cityname){
CityApi service = CityService.createCityService();
Subscription subscription = service.getCityInfo(cityname)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable error) {
}
@Override
public void onNext(City city) {
Log.d("CityInfo:",city.getCityCode() + "--" + city.getProvinceName());
}
});
}
結果:
D/CityInfo:: 101110101--陝西
以上就是簡單的使用方法。覺得好的多多點贊!