//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.slx.exam_classify.utils; import android.os.Vibrator; import com.example.slx.exam_classify.http.ApiService; import com.example.slx.exam_classify.http.ServiceUrl; import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by lenovo on 2018/4/20. */ public class HttpNetUtils { private static volatile HttpNetUtils instance; private final Retrofit retrofit; public static HttpNetUtils getInstance(){ if(instance==null){ synchronized (HttpNetUtils.class){ if(null==instance){ instance=new HttpNetUtils(); } } } return instance; } private HttpNetUtils(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(ServiceUrl.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } //retrofit自定义的接口 public ApiService getService(){ return retrofit.create(ApiService.class); } }
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() ; left.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() { } })
;
}}