使用retrofit结合rxAndroid实现简单的网络下载
添加依赖
// rx系列
compile 'io.reactivex.rxjava2:rxjava:2.1.5'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// 网络系列
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.google.code.gson:gson:2.8.2'
Retrofit工具类
public class RequestManager {
private static OkHttpClient client;
private static NetworkService networkService;
/**
* 普通不添加公共的头信息
*/
public static NetworkService getNormalService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Url.URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
networkService = retrofit.create(NetworkService.class);
return networkService;
}
/**
* 通过拦截器添加公共的头信息
*/
public static NetworkService getHeaderService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Url.URL)
.client(getHeaderClient())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
networkService = retrofit.create(NetworkService.class);
return networkService;
}
private static OkHttpClient getHeaderClient() {
client = new OkHttpClient.Builder().addInterceptor(new HeaderInterceptor()).build();
return client;
}
/**
* 头信息拦截器
*/
static class HeaderInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request request1 = request.newBuilder()
.addHeader("device_id", "597823023")
.addHeader("name", "macName")
.addHeader("interceptor", "拦截试试啊")
.build();
Response response = chain.proceed(request1);
return response;
}
}
}
Retrofit请求网络的API接口类
public interface NetworkService {
@GET("translator/api/v1/translator/history")
Observable<PreBuyOrderInfo> getPreBuyOrderInfo(@Query("user_id")String user_id);
}
实体类
public class PreBuyOrderInfo {
private int code;
private String msg;
private List<DataBean> data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
@Override
public String toString() {
return "DataBean{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", sn='" + sn + '\'' +
", mac='" + mac + '\'' +
'}';
}
private String name;
private String id;
private String sn;
private String mac;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
}
@Override
public String toString() {
return "PreBuyOrderInfo{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
实现方式
public class RetrofitAndRxAndroid2Activity extends AppCompatActivity {
@NonNull
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
private NetworkService retrofitInterface;
public static void startMe(Activity activity){
activity.startActivity(new Intent(activity,RetrofitAndRxAndroid2Activity.class));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_and_rx_android2);
retrofitInterface = RequestManager.getNormalService();
mCompositeDisposable.add(retrofitInterface.getPreBuyOrderInfo("ptqe1qfwzurugi8vblbqp8")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<PreBuyOrderInfo, List<PreBuyOrderInfo.DataBean>>() {
@Override
public List<PreBuyOrderInfo.DataBean> apply(@NonNull PreBuyOrderInfo preBuyOrderInfo) throws Exception {
return preBuyOrderInfo.getData();
}
})
.subscribe(new Consumer<List<PreBuyOrderInfo.DataBean>>() {
@Override
public void accept(List<PreBuyOrderInfo.DataBean> dataBean) throws Exception {
int size = dataBean.size();
for (int i = 0; i < size; i++) {
Log.e("tag","获取到的dataBean为:"+dataBean.get(i).toString()) ;
}
}
}));
}
}
半路猛虎
java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable<com.example.admin.example.bean.PreBuyOrderInfo>
在demo伊始,出现了上方的异常,经过多方查询发现是因为jakewharton大神所说的adapter-rxjava目前还不支持Rxjava 2.x,故引入jakewharton大神自己写了一个库让Retrofit来支持Rxjava 2.x。链接奉上:https://github.com/JakeWharton/retrofit2-rxjava2-adapter