学习 RxJava <二>与Retrofit结合
RxJava配合Retrofit 能非常轻松的从网络获取数据并显示到布局上
这里用 http://apistore.baidu.com/apiworks/servicedetail/113.html 写个例子
GET 请求地址 http://apis.baidu.com/apistore/idservice/id
Header apikey , Value f44bd76826571234e2616746eabf3c3c
URL Parameter Key id,Value 150105198001208417
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class IDCard { 2 3 4 @Override 5 public String toString() { 6 return "IDCard{" + 7 "errNum=" + errNum + 8 ", retMsg='" + retMsg + '\'' + 9 '}'; 10 } 11 12 /** 13 * errNum : 0 14 * retMsg : success 15 * retData : {"address":"湖北省孝感市汉川市","sex":"M","birthday":"1987-04-20"} 16 */ 17 18 private int errNum; 19 private String retMsg; 20 /** 21 * address : 湖北省孝感市汉川市 22 * sex : M 23 * birthday : 1987-04-20 24 */ 25 26 private RetDataBean retData; 27 28 public int getErrNum() { 29 return errNum; 30 } 31 32 public void setErrNum(int errNum) { 33 this.errNum = errNum; 34 } 35 36 public String getRetMsg() { 37 return retMsg; 38 } 39 40 public void setRetMsg(String retMsg) { 41 this.retMsg = retMsg; 42 } 43 44 public RetDataBean getRetData() { 45 return retData; 46 } 47 48 public void setRetData(RetDataBean retData) { 49 this.retData = retData; 50 } 51 52 public static class RetDataBean { 53 private String address; 54 private String sex; 55 private String birthday; 56 57 58 59 public String getAddress() { 60 return address; 61 } 62 63 public void setAddress(String address) { 64 this.address = address; 65 } 66 67 public String getSex() { 68 return sex; 69 } 70 71 public void setSex(String sex) { 72 this.sex = sex; 73 } 74 75 public String getBirthday() { 76 return birthday; 77 } 78 79 public void setBirthday(String birthday) { 80 this.birthday = birthday; 81 } 82 } 83 }
新建一个接口 IDCardApi
1 @GET("apistore/idservice/id") 2 rx.Observable<IDCard>getIDCardrx(@Header("apikey")String apikey, 3 @Query("id")String id 4 );
activity中 新建一个Retrofit 对象 并得到 IDCardApi 对象
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://apis.baidu.com/") .addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory
.create()) .build();
IDCardApi iDCardApi=retrofit.create(IDCardApi.class);
subscribeOn(Schedulers.io()) 表示subscribe()方法发生在io线程
observeOn(AndroidSchedulers.mainThread()) 表示Subscriber的回调发生在主线程
获取IDCard 对象,我要想要的是归属地,先判断获取数据是否正确,再获取string
后台线程取数据,主线程显示
不用手动开线程,更新ui也不需要用handle了‘,非常方便
iDCardApi.getIDCardrx("f44bd76826571234e2616746eabf3c3c", "150105198001208417") .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<IDCard,String>() { @Override public String call(IDCard idCard) { String xx; if (idCard != null && idCard.getErrNum() == 0) { xx = "地址是 " + idCard.getRetData().getAddress(); } else { xx = idCard.getErrNum() + idCard.getRetMsg(); } return xx; } }).subscribe(new Subscriber<String>() { @Override public void onCompleted() { progressBar.setVisibility(View.GONE); } @Override public void onError(Throwable e) { progressBar.setVisibility(View.GONE); textView.setText(e.getMessage()); } @Override public void onNext(String s) { textView.setText(s); } });