Rxprofile library is a well written network framework library, which is imported into its own project as a local Module first.
1. Most of its initialization operations are completed in user-defined application s, such as:
public class App extends Application { @Override public void onCreate() { super.onCreate(); //Initialize network framework library RxRetrofitApp.init(this,true); } }
2. Define a callback information unified encapsulation class (that is, the json entity class returned by the network request in the actual work) under rxresetlibrary. The generic T represents the Data class that users really care about, for example:
/** * Callback information unified encapsulation class */ public class BaseResultEntity<T> { // Judgement mark private int ret; // Prompt information private String msg; //Display data (data users need to care about) 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. Use the GsonFormat plug-in in the project to quickly create a simple Data entity class
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. Create the corresponding network request interface under the project (@ GET annotation with the parameter of network request url except BaseUrl, because BaseUrl is saved in the same encapsulation class of the request in 5, which is convenient for management)
public interface HttpGetService { @GET("url") Observable<BaseResultEntity<Data>> getData(); }
5. For the convenience of use, a request unified encapsulation class BaseApi is defined under rxconfiguration library. The generic type of Func1 interface implemented here is baseresultentity < T >, t. If in the actual project, it is to use the map operator of RxJava to convert baseresultentity < T > to t. (the transformation method focuses on the call method at the bottom of the code, which is determined according to the actual business logic.)
/** * Request unified encapsulation * @param <T> */ public abstract class BaseApi<T> implements Func1<BaseResultEntity<T>, T>{ //rx Life cycle management private SoftReference<RxAppCompatActivity> rxAppCompatActivity; /*Callback*/ private SoftReference<HttpOnNextListener> listener; /*Whether the loading box can be cancelled*/ private boolean cancel; /*Show load box or not*/ private boolean showProgress; /*Whether cache processing is required*/ private boolean cache; /*Foundation url*/ private String baseUrl = "http://192.168.0.101:8080/"; /*Method - this parameter must be set if caching is required; it is not required*/ private String mothed; /*Timeout - default 6 seconds*/ private int connectionTime = 6; /*Default local cache time with network is 60 seconds*/ private int cookieNetWorkTime = 60; /*Default local cache time is 30 days without network*/ private int cookieNoNetWorkTime = 24 * 60 * 60 * 30; /* retry times after failure*/ private int retryCount = 1; /*retry delay after failure*/ private long retryDelay = 100; /*retry stack delay after failure*/ 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); } /** * Setting parameters * * @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; } /* * Get the current rx lifecycle * @return */ public RxAppCompatActivity getRxAppCompatActivity() { return rxAppCompatActivity.get(); } @Override public T call(BaseResultEntity<T> httpResult) { //map Define conversion rules if (httpResult.getRet() == 0) {//0 Failed, 1 successful throw new HttpTimeException(httpResult.getMsg()); } return httpResult.getData(); } }
6. Create a subclass DataApi under the project to inherit the request unified encapsulation class in 5
public class DataApi extends BaseApi<Data> { public DataApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) { super(listener, rxAppCompatActivity); //Allowable cache setCache(true); //Cached flags setMothed("AppStore/test"); } @Override public Observable getObservable(Retrofit retrofit) { HttpGetService httpGetService = retrofit.create(HttpGetService.class); return httpGetService.getData(); } }
7. Finally, the network request data is obtained by calling from the sub Activity inherited from RxAppCompatActivity (because RxJava needs to control the life cycle).
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(); //Execute network request manager.doHttpDeal(api);