RetrofitUtils
package zjj.bwie.com.jx.utils;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private OkHttpClient okHttpClient;
private RetrofitUtils(){
okHttpClient = new OkHttpClient.Builder().build();
}
public static RetrofitUtils getInstence(){
if(retrofitUtils==null){
synchronized (RetrofitUtils.class){
if(retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
public BaseApiInterface getApi(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseApi.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
BaseApiInterface baseApiInterface = retrofit.create(BaseApiInterface.class);
return baseApiInterface;
}
}
Retrofit interface
package zjj.bwie.com.jx.utils;
import java.util.Map;
import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import zjj.bwie.com.jx.homepage.bean.HomePageBean;
import zjj.bwie.com.jx.me.bean.MeLoginBean;
import zjj.bwie.com.jx.shopcart.bean.ShopCartBean;
import zjj.bwie.com.jx.sort.bean.ChildSortBean;
import zjj.bwie.com.jx.sort.bean.SortBean;
public interface BaseApiInterface {
//获取首页bean类
@GET("ad/getAd")
Observable<HomePageBean> getHomePageBean();
//商品分类接口
@GET("product/getCatagory")
Observable<SortBean> getSortBean();
//商品子分类接口
@GET("product/getProductCatagory")
Observable<ChildSortBean> getChildShrtBean(@Query("cid") int cid);
//查询购物车
@GET("product/getCarts?uid=17403")
Observable<ShopCartBean> getShopCartBean();
//添加购物车
@GET("product/addCart?uid=17403")
Observable<ShopCartBean> addCart(@Query("pid") int pid);
//更新购物车
@GET("product/updateCarts?uid=17403")
Observable<ShopCartBean> updateCart(@QueryMap Map<String,Integer> map);
//删除购物车
@GET("product/deleteCart?uid=17403")
Observable<ShopCartBean> deleteCart(@Query("pid") int pid);
//维度登录
@FormUrlEncoded
@POST("login")
Observable<MeLoginBean> login(@Field("phone")String phone,@Field("pwd")String pwd);
//维度注册
@FormUrlEncoded
@POST("registerUser")
Observable<MeLoginBean> reg(@FieldMap Map<String,String> map);
}
v层
package zjj.bwie.com.jx.homepage.mvp.view;
import zjj.bwie.com.jx.homepage.bean.HomePageBean;
import zjj.bwie.com.jx.sort.bean.SortBean;
public interface HomePageContract {
public interface HomePageView{
void getData(HomePageBean homePageBean);
void getSortData(SortBean sortBean);
}
public interface HomePageModel{
interface OnRequestListenter{
void onSuccess(HomePageBean homePageBean);
void onSortSuccess(SortBean sortBean);
}
void requestData(OnRequestListenter listenter);
}
public interface HomePagePresenter{
void homePageData();
}
}