implementation 'com.squareup.okhttp3:okhttp:3.1.2'
implementation 'com.facebook.fresco:fresco:0.12.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.0.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
接口:
public interface Api {
@POST("small/user/v1/register")
Observable<LoginBean> Register(@QueryMap HashMap<String, String> hashMap);
@GET("small/commodity/v1/commodityList")
Observable<HomeBean> show();
@GET("small/commodity/v1/findCommodityDetailsById")
Observable<XqBean> Xiangq(@Query("commodityId")String id);
}
工具类:
public class HttpUtil<T> {
public static HttpUtil getInstance(){
return httpUtilInter.httpUtil;
}
private static class httpUtilInter{
private static HttpUtil httpUtil=new HttpUtil();
}
public final Api api;
private HttpUtil() {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://api.tianapi.com/")
.build();
api = retrofit.create(Api.class);
}
}
public class MainPresenter extends BasePresenter<IShowView>{
private final HttpUtil httpUtils;
public MainPresenter() {
httpUtils = HttpUtil.getInstance();
}
public void loadDataFromNet(){
Observable<HomeBean> show = httpUtils.api.show();
show.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<HomeBean>() {
@Override
public void accept(HomeBean homeBean) throws Exception {
getView().onSugess(homeBean);
}
});
}