前言
Retrofit 和RxJava已经出来很久了,很多前辈写了很多不错的文章,在此不得不感谢这些前辈无私奉献的开源精神,能让我们站在巨人的肩膀上望得更远。对于 RxJava 不是很了解的同学推荐你们看扔物线大神的这篇文章给 Android 开发者的 RxJava 详解一遍看不懂就看第二遍。Retrofit的使用可以参考Android Retrofit 2.0使用
本文内容是基于Retrofit + RxJava做的一些巧妙的封装。参考了很多文章加入了一些自己的理解,请多指教。源码地址https://github.com/Hemumu/RxSample
帮助类
public class RetrofitHelper {
private static RetrofitHelper instance;
private Context context;
private String url;
private Retrofit mRetrofit;
OkHttpClient client = new OkHttpClient();
//单例模式
public static RetrofitHelper getInstance(Context context,String url){
instance = new RetrofitHelper(context,url);
return instance;
}
//创建gson工厂
GsonConverterFactory factory = GsonConverterFactory.create(new Gson());
public RetrofitHelper(Context context,String url) {
this.context = context;
init(url);
}
private void init(String url) {
resetApp(url);
}
private void resetApp(String url) {
mRetrofit = new Retrofit.Builder()
.baseUrl(url)
.client(client)
.addConverterFactory(factory)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
public RetrofitService getServer() {
return mRetrofit.create(RetrofitService.class);
}
}
定义服务
public interface RetrofitService {
@POST("product/getCarts")
Observable<GoodsBean> getGoodsCar(
@QueryMap Map<String, String> params
);
@POST("product/addCart")
Observable<AddCarBean> getAddCarData(
@QueryMap Map<String, String> params
);
}
// 购物车 public static String GwChe_URL = "https://www.zhaoapi.cn/";
modle层
public interface IModle {
// 购物车
void showGoods(String url, Map<String,String> parms, Context context, ShoppingCarLinstener shoppingCarLinstener);
// 搜索
void getSearchDatas(String url , Map<String,String> params, Context context,SYSearchListner searchListner);
// 添加购物车
void getAddCar(String url , Map<String,String> params, Context context,AddCarListner addCarListner);
}
成功监听-------
public interface ShoppingCarLinstener {
void getDataSuccess(List<GoodsBean.DataBean> beanList);
void getDataError(String error);
}
modle实现类-------------------------------
public class ModleImp implements IModle {
// 购物车
@Override
public void showGoods(String url, Map<String, String> params, Context context, final ShoppingCarLinstener shoppingCarLinstener) {
RetrofitHelper instance = RetrofitHelper.getInstance(context, url);
RetrofitService server = instance.getServer();
Observable<GoodsBean> goodsCar = server.getGoodsCar(params);
goodsCar.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<GoodsBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
shoppingCarLinstener.getDataError(e.getMessage());
}
@Override
public void onNext(GoodsBean goodsBean) {
List<GoodsBean.DataBean> beanList = goodsBean.getData();
shoppingCarLinstener.getDataSuccess(beanList);
}
});
}
@Override
public void getAddCar(String url, Map<String, String> params, Context context, final AddCarListner addCarListner) {
RetrofitHelper instance = RetrofitHelper.getInstance(context, url);
RetrofitService server = instance.getServer();
Observable<AddCarBean> addCarData = server.getAddCarData(params);
addCarData.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<AddCarBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
addCarListner.getAddCarError(e.getMessage());
}
@Override
public void onNext(AddCarBean addCarBean) {
String msg = addCarBean.getMsg();
addCarListner.getAddCarSuccess(msg);
}
});
}
}
view层————————————————–
public interface IGwCheView {
void showGowWuList(List<GoodsBean.DataBean> data);
//显示总价
void showCount(String count);
}
present层—————————————————————–
public interface IPresenter {
// 购物车
void showShoppingCarToView(IModle imodle, Context context, IGwCheView iGwCheView ,int uid);
// 添加购物车
void showAddCarToView(IModle imodle, Context context, IAddCarView iAddCarView, int Uid, int Pid);
}
p层实现类------------------------------------------------------------------
public class PresenterImp implements IPresenter {
//购物车
@Override
public void showShoppingCarToView(IModle imodle, Context context, final IGwCheView iGwCheView,int uid) {
Map<String, String> params = new HashMap<>();
params.put("source", "android");
params.put("uid",uid+"");
// params.put("uid","12007" );
imodle.showGoods(HttpConfig.GwChe_URL, params, context, new ShoppingCarLinstener() {
@Override
public void getDataSuccess(List<GoodsBean.DataBean> beanList) {
iGwCheView.showGowWuList(beanList);
}
@Override
public void getDataError(String error) {
Log.d("P---购物error---", error);
}
});
}
@Override
public void showAddCarToView(IModle imodle, Context context, final IAddCarView iAddCarView, int Uid, int Pid) {
Map<String, String> params = new HashMap<>();
params.put("source","android");
params.put("Uid",Uid+"");
params.put("Pid",Pid+"");
imodle.getAddCar(HttpConfig.Detail_URL, params, context, new AddCarListner() {
@Override
public void getAddCarSuccess(String msg) {
iAddCarView.showAddCarMsg(msg);
}
@Override
public void getAddCarError(String error) {
Log.d("P----加入购物车-------", error);
}
});
}
// 解决内存泄漏,activity中调用
public void onDestroy(IView iView){
iView = null;
}
}