rxretrofitlibrary是一个已经写好的网络框架库,先以本地Module导入到自己的项目中。
1、它的初始化操作大多在自定义的application中完成,如:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class App extends Application { @Override public void onCreate() { super.onCreate(); //初始化网络框架库 RxRetrofitApp.init(this,true); } }
2、在rxretrofitlibrary下定义一个回调信息统一封装类(即实际工作中网络请求的返回json实体类),泛型T表示用户真正关心的数据Data类,例如:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 回调信息统一封装类 */ public class BaseResultEntity<T> { // 判断标示 private int ret; // 提示信息 private String msg; //显示数据(用户需要关心的数据) private T data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getRet() { return ret; } public void setRet(int ret) { this.ret = ret; } }
3、在项目中使用GsonFormat插件快速创建简单的Data实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Data { private String downurl; private String icoUri; private String name; private String packageName; public String getDownurl() { return downurl; } public void setDownurl(String downurl) { this.downurl = downurl; } public String getIcoUri() { return icoUri; } public void setIcoUri(String icoUri) { this.icoUri = icoUri; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } @Override public String toString() { return "name:"+name+",packageName:"+packageName ; } }
4、在项目下创建相应的网络请求接口(@GET注解后带的参数为网络请求url除BaseUrl部分,因为BaseUrl保存在了5中的请求同一封装类中,便于管理)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public interface HttpGetService { @GET("url") Observable<BaseResultEntity<Data>> getData(); }
5、为了使用方便,在rxretrofitlibrary下再定义一个请求统一封装类BaseApi,此处实现的Func1接口泛型为BaseResultEntity<T>, T。如果在实际项目中,是为了使用RxJava的map操作符将BaseResultEntity<T>转换为T。(转换的方法重点在代码最下面的call方法中,根据实际的业务逻辑来定)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 请求统一封装 * @param <T> */ public abstract class BaseApi<T> implements Func1<BaseResultEntity<T>, T>{ //rx生命周期管理 private SoftReference<RxAppCompatActivity> rxAppCompatActivity; /*回调*/ private SoftReference<HttpOnNextListener> listener; /*是否能取消加载框*/ private boolean cancel; /*是否显示加载框*/ private boolean showProgress; /*是否需要缓存处理*/ private boolean cache; /*基础url*/ private String baseUrl = "http://192.168.0.101:8080/"; /*方法-如果需要缓存必须设置这个参数;不需要不用設置*/ private String mothed; /*超时时间-默认6秒*/ private int connectionTime = 6; /*有网情况下的本地缓存时间默认60秒*/ private int cookieNetWorkTime = 60; /*无网络的情况下本地缓存时间默认30天*/ private int cookieNoNetWorkTime = 24 * 60 * 60 * 30; /* 失败后retry次数*/ private int retryCount = 1; /*失败后retry延迟*/ private long retryDelay = 100; /*失败后retry叠加延迟*/ private long retryIncreaseDelay = 10; public BaseApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) { setListener(listener); setRxAppCompatActivity(rxAppCompatActivity); setShowProgress(false); setCache(false); setCancel(true); setCookieNetWorkTime(60); setCookieNoNetWorkTime(24*60*60); } /** * 设置参数 * * @param retrofit * @return */ public abstract Observable getObservable(Retrofit retrofit); public int getCookieNoNetWorkTime() { return cookieNoNetWorkTime; } public void setCookieNoNetWorkTime(int cookieNoNetWorkTime) { this.cookieNoNetWorkTime = cookieNoNetWorkTime; } public int getCookieNetWorkTime() { return cookieNetWorkTime; } public void setCookieNetWorkTime(int cookieNetWorkTime) { this.cookieNetWorkTime = cookieNetWorkTime; } public String getMothed() { return mothed; } public int getConnectionTime() { return connectionTime; } public void setConnectionTime(int connectionTime) { this.connectionTime = connectionTime; } public void setMothed(String mothed) { this.mothed = mothed; } public String getBaseUrl() { return baseUrl; } public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } public String getUrl() { return baseUrl + mothed; } public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) { this.rxAppCompatActivity = new SoftReference(rxAppCompatActivity); } public boolean isCache() { return cache; } public void setCache(boolean cache) { this.cache = cache; } public boolean isShowProgress() { return showProgress; } public void setShowProgress(boolean showProgress) { this.showProgress = showProgress; } public boolean isCancel() { return cancel; } public void setCancel(boolean cancel) { this.cancel = cancel; } public SoftReference<HttpOnNextListener> getListener() { return listener; } public void setListener(HttpOnNextListener listener) { this.listener = new SoftReference(listener); } public int getRetryCount() { return retryCount; } public void setRetryCount(int retryCount) { this.retryCount = retryCount; } public long getRetryDelay() { return retryDelay; } public void setRetryDelay(long retryDelay) { this.retryDelay = retryDelay; } public long getRetryIncreaseDelay() { return retryIncreaseDelay; } public void setRetryIncreaseDelay(long retryIncreaseDelay) { this.retryIncreaseDelay = retryIncreaseDelay; } /* * 获取当前rx生命周期 * @return */ public RxAppCompatActivity getRxAppCompatActivity() { return rxAppCompatActivity.get(); } @Override public T call(BaseResultEntity<T> httpResult) { //map 定义转换规则 if (httpResult.getRet() == 0) {//0失败,1成功 throw new HttpTimeException(httpResult.getMsg()); } return httpResult.getData(); } }
6、在项目下创建子类DataApi来继承5中的请求统一封装类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class DataApi extends BaseApi<Data> { public DataApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) { super(listener, rxAppCompatActivity); //允许缓存 setCache(true); //缓存的标志 setMothed("AppStore/test"); } @Override public Observable getObservable(Retrofit retrofit) { HttpGetService httpGetService = retrofit.create(HttpGetService.class); return httpGetService.getData(); } }
7、最终在继承自RxAppCompatActivity(因为RxJava需要控制生命周期)的子Activity中调用来得到网络请求数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DataApi api = new DataApi(new HttpOnNextListener<Data>() { @Override public void onNext(Data data) { Log.i("MainActivity","onNext:"+data.toString()) ; Log.i("MainActivity","downurl:"+data.getDownurl()); } @Override public void onCacheNext(String string) { super.onCacheNext(string); Log.i("MainActivity","onCache:"+string); } @Override public void onError(Throwable e) { super.onError(e); } },this); HttpManager manager = HttpManager.getInstance(); //执行网络请求 manager.doHttpDeal(api);