//okhttp请求
compile 'com.squareup.okhttp3:okhttp:3.10.0'
//拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
//Gson依赖
compile 'com.google.code.gson:gson:2.2.4'
//retrofit依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
// RxJava依赖implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
package com.example.lenovo.dsapp.utils; import com.example.lenovo.dsapp.service.ApiService; import com.example.lenovo.dsapp.service.ServiceUrl; import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * 请求数据的工具类 */ public class HttpNetWork { private static volatile HttpNetWork instance; private final Retrofit retrofit; private ApiService apiService; private HttpNetWork(){ HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient=new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(ServiceUrl.BASE_URL) .client(okHttpClient) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); } public static HttpNetWork getInstance(){ if (instance==null){ synchronized (HttpNetWork.class){ if (null==instance){ instance=new HttpNetWork(); } } } return instance; } public ApiService getService(){ if (apiService == null){ apiService = retrofit.create(ApiService.class); } return apiService; } }
http
package com.example.slx.exam_classify.http; /** * Created by lenovo on 2018/4/20. */ public class ServiceUrl { public static final String BASE_URL = "https://www.zhaoapi.cn/product/"; }
package com.example.slx.exam_classify.http; import com.example.slx.exam_classify.bean.LeftBean; import com.example.slx.exam_classify.bean.MessageBean; import com.example.slx.exam_classify.bean.RightBean; import com.example.slx.exam_classify.bean.RightBeanList; import io.reactivex.Flowable; import retrofit2.http.GET; import retrofit2.http.Query; /** * fetrofit的接口定义 * Created by lenovo on 2018/4/20. */ public interface ApiService { @GET("getCatagory") Flowable<MessageBean<LeftBean>> getData1() ; @GET("getProductCatagory") Flowable<MessageBean<RightBean<RightBeanList>>> getData2(@Query("cid")String cid); }
调用
HttpNetUtils:
public class Model {
public void getData(final IPresenter iPresenter) {
//调用工具类,得到被观察者
final HttpNetUtils instance = HttpNetUtils.getInstance();
ApiService service = instance.getService();
Flowable<MessageBean<LeftBean>> data = service.getData();
data.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
//创建被观察者对象
.subscribe(new DisposableSubscriber<MessageBean<LeftBean>>() {
@Override public void onNext(MessageBean<LeftBean> leftBeanMessageBean) {
iPresenter.success(leftBeanMessageBean);
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
} }); }}