前一篇博客讲了怎么通过retrofit库获取网络数据,最近在看RxJava,所以自己动手,参考了一些网上的资料,写了一个小的演示程序。
本次的程序功能和前面一样,利用聚合数据接口获取号码归属地,其接口和返回数据格式如下:
首先是添加依赖,这里需要注意的是retrofit和adapter-rxjava的版本,第一次我采用retrofit2.0.3和adapter-rxjava2.0.0-bata4时总是报错,后来改了retrofit版本就好了:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxjava:1.1.4'通过返回的json格式建立一个Bean类,解析返回的Json,可以采用GsonFormat插件自动生成。这里我把它命名为PhoneNumInfo,具体内容如下:
public class PhoneNumInfo { /** * resultcode : 200 * reason : Return Successd! * result : {"province":"浙江","city":"杭州","areacode":"0571","zip":"310000","company":"中国移动","card":"移动动感地带卡"} */ private String resultcode; private String reason; /** * province : 浙江 * city : 杭州 * areacode : 0571 * zip : 310000 * company : 中国移动 * card : 移动动感地带卡 */ private ResultBean result; public String getResultcode() { return resultcode; } public void setResultcode(String resultcode) { this.resultcode = resultcode; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } public ResultBean getResult() { return result; } public void setResult(ResultBean result) { this.result = result; } public static class ResultBean { private String province; private String city; private String areacode; private String zip; private String company; private String card; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } } }接下来就是retrofit的请求接口了,和常规不同的时,采用Rxjava时,返回的是Observable<>,不再是Call<>;
public interface Phone { @Headers("Content-Type:application/json") @GET("mobile/get") Observable<PhoneNumInfo> getHaoMa(@Query("phone") String phone, @Query("key") String key); }最后就是获取数据了
Retrofit retrofit=new Retrofit.Builder() .baseUrl("http://apis.juhe.cn/") .addConverterFactory(GsonConverterFactory.create())//自动将json转换为 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); Phone phone=retrofit.create(Phone.class); phone.getHaoMa(meditview.getText().toString(), key) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<PhoneNumInfo>() { @Override public void onCompleted() { Log.i("onCompleted", "onCompleted"); } @Override public void onError(Throwable e) { Log.i("onError", e.getMessage()); } @Override public void onNext(PhoneNumInfo phoneNumInfo) { mtext.setText(phoneNumInfo.getResult().getCity()); } });结果演示: