Retrofit和Rxjava结合使用
1.添加依赖:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
2.Retrofit的使用:
1).编写API服务代码
package com.micky.retrofitrxandroiddagger2.data.api;
import com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse;
import retrofit.Call;
import retrofit.http.GET;
import retrofit.http.Query;
import rx.Observable;
/**
* @Project retrofitrxandroiddagger2
* @Packate com.micky.retrofitrxandroiddagger2.data.api
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-21 17:22
* @Version 1.0
*/
public interface ApiService {
@GET("service/getIpInfo.php")
Call<MyMessage> getIpInfo(@Body FprmBody body);
}
2).使用GsonFormat定义接收数据的Bean类
public class MyMessage {
/**
* reason : 成功的返回
* result : {"code":100000,"text":"答不答就看我心情啦。"}
* error_code : 0
*/
private String reason;
private ResultBean result;
private int error_code;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
3).网络请求数据
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://op.juhe.cn/robot/index/")
.client(new OkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
FormBody body = new FormBody.Builder()
.add("info","%E6%88%91%E6%98%AF%E8%B0%81&")
.add("dtype","&loc=&userid=&")
.add("key","f5d2667dff1bfc58786a06c04c8429af").build();
netAPI= retrofit.create(NetAPI.class);
netAPI.Message(body).enqueue(new Callback<MyMessage>() {
@Override
public void onResponse(Call<MyMessage> call, Response<MyMessage> response) {
}
@Override
public void onFailure(Call<MyMessage> call, Throwable t) {
}
});
3.Retrofit和Rxjava结合使用
1).首先在初始化Retrofit中加入addCallAdapterFactory(RxjavaCallAdapterFactory.create())
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://op.juhe.cn/robot/index/")
.client(new OkHttpClient())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
2).API类中Call变为Observable
Observable<MyMessage> Message(@Body FormBody body);
3).具体代码
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://op.juhe.cn/robot/index/")
.client(new OkHttpClient())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
FormBody body = new FormBody.Builder()
.add("info","%E6%88%91%E6%98%AF%E8%B0%81&")
.add("dtype","&loc=&userid=&")
.add("key","f5d2667dff1bfc58786a06c04c8429af").build();
netAPI= retrofit.create(NetAPI.class);
netAPI.Message(body).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MyMessage>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.e(TAG,"onsubscribe");
}
@Override
public void onNext(@NonNull MyMessage myMessage) {
Log.e(TAG,"onNext");
tx.setText(myMessage.getResult().getText());
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG,"onError");
}
@Override
public void onComplete() {
Log.e(TAG,"onComplete");
}
});
}
})
不同之处在于加入三个方法:
subscribeOn(Schedulers.io):指定Observable的工作, 在我们的例子中Observable的工作即发送请求, 在io线程做, 指定了被观察者的处理线程;
observeOn(AndroidSchedulers.mainThread()):指定最后onNext()回调在主线程, 即指定了通知后续观察者的线程.
subscribe():添加一个订阅者, 即它的观察者.当请求返回后, 回到主线程, 更新UI.