// Okhttp库
compile 'com.squareup.okhttp3:okhttp:3.1.2'
rxJava依赖包:
// Retrofit库
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
//《RXjava2的适配器》
compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
//《Rxjava2》
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
package com.example.jd.model.untils; import com.example.jd.model.bean.ProductListBeans; import com.example.jd.model.bean.SouBeans; import io.reactivex.Observable; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query; /** * Created by nyj on 2018/5/16. */ public interface RetrofitApi { @GET("product/getProducts") Observable<ProductListBeans> getCall(@Query("pscid") String pscid); @POST("product/searchProducts") @FormUrlEncoded Observable<SouBeans> getSou(@Field("keywords") String keywords); }
package com.example.jd.model.untils; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by nyj on 2018/5/16. */ public class RetrofitUntils { private Retrofit retrofit; private RetrofitUntils retrofitUntils; private RetrofitUntils() { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .build(); //建立retrofit对象 retrofit = new Retrofit.Builder() .baseUrl(Constant.baseUrl) .client(client) .addConverterFactory(GsonConverterFactory.create())//默认gson解析 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用RxJava2的适配器 .build(); } //单列模式 public static RetrofitUntils INSTANCE; public static RetrofitUntils getInstance() { if (INSTANCE == null) { synchronized (RetrofitUntils.class) { if (INSTANCE == null) { INSTANCE = new RetrofitUntils(); } } } return INSTANCE; } //创建方法,供调用 public RetrofitApi getApi() { //获取接口 RetrofitApi retrofitApi = retrofit.create(RetrofitApi.class); return retrofitApi; } }
package com.example.jd.presenter; import android.util.Log; import com.example.jd.model.bean.ProductListBeans; import com.example.jd.model.https.HttpCallback; import com.example.jd.model.untils.RetrofitApi; import com.example.jd.model.untils.RetrofitUntils; import com.example.jd.view.interfaces.IProListView; import com.google.gson.Gson; import java.util.List; import io.reactivex.Observable; import io.reactivex.Scheduler; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.observers.DisposableObserver; import io.reactivex.schedulers.Schedulers; import retrofit2.Call; /** * Created by nyj on 2018/5/16. */ public class ProductListPresenter extends BasePresenter<IProListView> { private final RetrofitUntils retrofitUntils; public ProductListPresenter() { retrofitUntils = RetrofitUntils.getInstance(); } //加载数据 public void loadListData(String pscid) { Observable<ProductListBeans> observable = retrofitUntils.getApi().getCall(pscid); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ProductListBeans>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ProductListBeans productListBeans) { List<ProductListBeans.DataBean> data = productListBeans.getData(); Log.e("value", productListBeans.getData().get(0).getDetailUrl() + ""); getView().onSuccess(data); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });}}