rxretrofitlibrary是一個已經寫好的網路框架庫,先以本地Module導入到自己的項目中。 1、它的初始化操作大多在自定義的application中完成,如: public class App extends Application { @Override public void onC ...
rxretrofitlibrary是一個已經寫好的網路框架庫,先以本地Module導入到自己的項目中。
1、它的初始化操作大多在自定義的application中完成,如:
public class App extends Application { @Override public void onCreate() { super.onCreate(); //初始化網路框架庫 RxRetrofitApp.init(this,true); } }View Code
2、在rxretrofitlibrary下定義一個回調信息統一封裝類(即實際工作中網路請求的返回json實體類),泛型T表示用戶真正關心的數據Data類,例如:
/** * 回調信息統一封裝類 */ 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; } }View Code
3、在項目中使用GsonFormat插件快速創建簡單的Data實體類
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 ; } }View Code
4、在項目下創建相應的網路請求介面(@GET註解後帶的參數為網路請求url除BaseUrl部分,因為BaseUrl保存在了5中的請求同一封裝類中,便於管理)
public interface HttpGetService { @GET("url") Observable<BaseResultEntity<Data>> getData(); }View Code
5、為了使用方便,在rxretrofitlibrary下再定義一個請求統一封裝類BaseApi,此處實現的Func1介面泛型為BaseResultEntity<T>, T。如果在實際項目中,是為了使用RxJava的map操作符將BaseResultEntity<T>轉換為T。(轉換的方法重點在代碼最下麵的call方法中,根據實際的業務邏輯來定)
/** * 請求統一封裝 * @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(); } }View Code
6、在項目下創建子類DataApi來繼承5中的請求統一封裝類
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(); } }View Code
7、最終在繼承自RxAppCompatActivity(因為RxJava需要控制生命周期)的子Activity中調用來得到網路請求數據
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);View Code