标签:ada jdk getc and abstract rac let figure end
入职公司后,公司要求组件化开发,经过讨论后我将网络请求框架单独进行了封装,不过当时框架里将常用的 util 和 ui 均放入到了共同的 Common 包下,导致里面部分代码耦合,后来为了降低耦合性又将 Common 拆分为了lib_common和lib_ui,但是 lib_ui 依赖了 lib_common,还是导致部分代码耦合,最新一期为了降低组件之间的耦合性,所以单独将 lib_common 中的网络请求单独拆分,并且我又做了新的封装和完善,总之网络框架经过3次大的改造后,使用已经非常稳定了。
1.在Application类中进行初始化操作
?ApiConfig?build?=?new?ApiConfig.Builder()
?????????????????.setBaseUrl(baseUrl)//BaseUrl,这个地方加入后项目中默认使用该url
????????????????.setInvalidateToken(0)//Token失效码
????????????????.setSucceedCode(200)//成功返回码
????????????????.setFilter("com.mp5a5.quit.broadcastFilter")//失效广播Filter设置
????????????????//.setDefaultTimeout(2000)//响应时间,可以不设置,默认为2000毫秒
????????????????//.setHeads(headMap)//动态添加的header,也可以在其他地方通过ApiConfig.setHeads()设置
????????????????//.setOpenHttps(true)//开启HTTPS验证
????????????????//.setSslSocketConfigure(sslSocketConfigure)//HTTPS认证配置
????????????????.build();
?build.init(this);
2.定义接口
?public?interface?NBAApiT?{
????@GET("onebox/basketball/nba")
????Observable<NBAEntity>?getNBAInfo(@QueryMap?ArrayMap<String,?Object>?map);
}
3.创建请求实例
单例模式创建Service,推荐使用这种
public?class?NbaService?{
????private?NBAApiT?nbaApiT;
????private?NbaService()?{
????????nbaApiT?=?RetrofitFactory.getInstance().create(NBAApiT.class);
????}
????public?static?NbaService?getInstance()?{
????????return?Nbaservice1Holder.S_INSTANCE;
????}
????private?static?class?Nbaservice1Holder?{
????????private?static?final?NbaService?S_INSTANCE?=?new?NbaService();
????}
????public?Observable<NBAEntity>?getNBAInfo(String?key)?{
????????ArrayMap<String,?Object>?map?=?new?ArrayMap<>();
????????map.put("key",?key);
????????return?nbaApiT.getNBAInfo(map);
????}
}
4.发送请求
findViewById(R.id.btnNBA).setOnClickListener(v?->?{
????????????NbaService.getInstance()
????????????????????.getNBAInfo("6949e822e6844ae6453fca0cf83379d3")
????????????????????.subscribeOn(Schedulers.io())
????????????????????.observeOn(AndroidSchedulers.mainThread())
????????????????????.compose(this.bindToLifecycle())
????????????????????.subscribe(new?BaseObserver<NBAEntity>(){
????????????????????????@Override
????????????????????????public?void?onSuccess(NBAEntity?response)?{
????????????????????????????Toast.makeText(TestNBAActivity.this,?response.result.title,?Toast.LENGTH_SHORT).show();
????????????????????????}
????????????????????});
????????});
5.效果展示
返回参数回调
由于 JDK1.8 中接口可以有默认不需要实现的方法,所以我采用了JDK1.8的新特新封装了网络请求返回参数的回调。这样做的好处就是有些情况下,我们是只需要处理成功的需求,但是失败和错误我们并不是太关心,所以在观察者类 BaseObserver 中我对失败和错误做了统一的封装,这样我们可以不需要每次写回调参数的时候,都去处理失败和错误。大大的减轻了代码量。
public?interface?OnBaseResponseListener?{
??????void?onSuccess(R?response);
??????default?void?onFailing(R?response)?{}
??????default?void?onError()?{}
}
其中 onSuccess() 方法是必须实现的,onFailing(R response) 和 onError() 可以不用实现,如果项目中想处理网络请求失败和错误,则需要重写onFailing(R response) 和 onError()方法,如果用到了封装的 loading 框和Toast,则需要 super.onFailing(response) 和 super.onError(e),否则则可以不用 super。
这个类是所有用到网络请求实体类的父类,根据这个类中的code,我们在观察者类BaseObserver 中判断网络请求是成功还是失败或者token失效。
public?class?BaseResponseEntity?implements?Serializable?{
??private?static?final?long?serialVersionUID?=?1L;
??public?int?code;
??public?String?msg;
??public?boolean?success()?{
????return?ApiConfig.getSucceedCode()?==?code;
??}
??public?int?getTokenInvalid()?{
????return?ApiConfig.getInvalidateToken();
??}
}
例如这个请求NBA返回的实体类,我们只需要继承自 BaseResponseEntity。
public?class?NBAEntity?extends?BaseResponseEntity?{
????@SerializedName("error_code")
????public?int?code;
????public?String?reason;
????public?ResultBean?result;
????public?static?class?ResultBean?{
??????public?String?title;
??????}
}
由于我的项目返回的 code 码这个字段并不是 BaseResponseEntity 中的 code,所以可以采用起别名的方式,
????@SerializedName("error_code")
????public?int?code;
这样就可以解决公司返回的code码字段和我封装的字段不相同的问题,当然每个bean都写这段代码显然不是特别友好,所以你可以再封装一个bean继承自BaseResponseEntity,然后给code起别名就可以了,那样项目中的其他的bean只需要继承你自己封装的bean。
这个类继承自rxjava中的观察者类Observer,这个类中我在onNext()方法中对返回参数进行判断,如果code是成功的code码表示本次网络请求是成功的,如果code不是成功的code码,那代表网络请求是失败的,对失败做了统一封装处理,如果返回的code值为token失效,这样我发送了一条动态广播,在自己的项目中,你只要在activity中的基类中接收该动态广播,然后做退出登录、清空数据等操作,同样在onError()方法中,我对错误做了处理。在上面我已经提示过了,失败onFailing(response)和错误onError(Throwable e)方法可以不用实现的。因为我在这个类已经做了统一处理。
public?abstract?class?BaseObserver<T?extends?BaseResponseEntity>?implements?Observer<T>?{
????、、、
????@Override
????public?void?onNext(T?response)?{
????????if?(response.success())?{
????????????try?{
????????????????onSuccess(response);
????????????}?catch?(Exception?e)?{
????????????????e.printStackTrace();
????????????}
????????}?else?if?(response.getTokenInvalid()?==?response.code)?{
????????????//token失效捕捉,发送广播,在项目中接收该动态广播然后做退出登录等一些列操作
????????????Intent?intent?=?new?Intent();
????????????intent.setAction(ApiConfig.getQuitBroadcastReceiverFilter());
????????????intent.putExtra(TOKEN_INVALID_TAG,?QUIT_APP);
????????????AppContextUtils.getContext().sendBroadcast(intent);
????????}?else?{
????????????try?{
????????????????onFailing(response);
????????????}?catch?(Exception?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????}
????@Override
????public?void?onError(Throwable?e)?{
????????if?(e?instanceof?retrofit2.HttpException)?{
????????????//HTTP错误
????????????onException(ExceptionReason.BAD_NETWORK);
????????}?else?if?(e?instanceof?ConnectException?||?e?instanceof?UnknownHostException)?{
????????????//连接错误
????????????onException(ExceptionReason.CONNECT_ERROR);
????????}?else?if?(e?instanceof?InterruptedIOException)?{
????????????//连接超时
????????????onException(ExceptionReason.CONNECT_TIMEOUT);
????????}?else?if?(e?instanceof?JsonParseException?||?e?instanceof?JSONException?||?e?instanceof?ParseException)?{
????????????//解析错误
????????????onException(ExceptionReason.PARSE_ERROR);
????????}?else?{
????????????//其他错误
????????????onException(ExceptionReason.UNKNOWN_ERROR);
????????}
????}
????、、、
????@Override
????public?void?onComplete()?{
???????、、、
????}
????public?abstract?void?onSuccess(T?response);
????public?void?onFailing(T?response)?{
????????String?message?=?response.msg;
????????if?(TextUtils.isEmpty(message))?{
????????????Toast.makeText(AppContextUtils.getContext(),?RESPONSE_RETURN_ERROR,?Toast.LENGTH_SHORT).show();
????????}?else?{
????????????Toast.makeText(AppContextUtils.getContext(),?message,?Toast.LENGTH_SHORT).show();
????????}
????}
????、、、
}
这个类是配合okttp、Gson、拦截器等,对Retrofit进行的封装。这里对请求超时的时间,请求头拦截器、请求缓存大小、日志拦截器、https认证、返回json处理等、都在这个类做了处理,可以说这个类是处理Retrofit的核心类。
public?class?RetrofitFactory?{
????、、、
????private?RetrofitFactory()?{
????????//?指定缓存路径,缓存大小100Mb
????????File?cacheFile?=?new?File(AppContextUtils.getContext().getCacheDir(),?"HttpCache");
????????Cache?cache?=?new?Cache(cacheFile,?1024?*?1024?*?100);
????????OkHttpClient.Builder?httpClientBuilder?=?new?OkHttpClient().newBuilder()
????????????????.readTimeout(ApiConfig.getDefaultTimeout(),?TimeUnit.MILLISECONDS)
????????????????.connectTimeout(ApiConfig.getDefaultTimeout(),?TimeUnit.MILLISECONDS)
????????????????.addInterceptor(HttpLoggerInterceptor.getLoggerInterceptor())
????????????????.addInterceptor(new?HttpHeaderInterceptor())
????????????????.addNetworkInterceptor(new?HttpCacheInterceptor())
????????????????.cache(cache);
????????if?(ApiConfig.getOpenHttps())?{
????????????httpClientBuilder.sslSocketFactory(1?==?ApiConfig.getSslSocketConfigure().getVerifyType()??
????????????????????SslSocketFactory.getSSLSocketFactory(ApiConfig.getSslSocketConfigure().getCertificateInputStream())?:
????????????????????SslSocketFactory.getSSLSocketFactory(),?new?UnSafeTrustManager());
????????????httpClientBuilder.hostnameVerifier(new?UnSafeHostnameVerify());
????????}
????????OkHttpClient?httpClient?=?httpClientBuilder.build();
????????Gson?gson?=?new?GsonBuilder()
????????????????.setDateFormat("yyyy-MM-dd?HH:mm:ss")
????????????????.serializeNulls()
????????????????.registerTypeAdapterFactory(new?NullTypeAdapterFactory())
????????????????.create();
????????retrofit?=?new?Retrofit.Builder()
????????????????.client(httpClient)
????????????????.addConverterFactory(GsonConverterFactory.create(gson))
????????????????.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
????????if?(!TextUtils.isEmpty(ApiConfig.getBaseUrl()))?{
????????????build?=?retrofit.baseUrl(ApiConfig.getBaseUrl()).build();
????????}
????}
????、、、
????public?<T>?T?create(Class<T>?clazz)?{
????????checkNotNull(build,?"BaseUrl?not?init,you?should?init?first!");
????????return?build.create(clazz);
????}
????public?<T>?T?create(String?baseUrl,?Class<T>?clazz)?{
????????return?retrofit.baseUrl(baseUrl).build().create(clazz);
????}
}
这个类是对所有初始化参数进行配置的地方,比如返回码 code,BaseUrl,失效InvalidateToken,请求头 Heads,是否开启 https 认证等的一系配置。可以在项目的 application 中对自己需要的参数进行配置,这样项目中只需写请求相关的代码,而不需要处理请求baseUrl、返回code等繁琐的任务。
public?class?ApiConfig?implements?Serializable?{
????private?static?int?mInvalidateToken;
????private?static?String?mBaseUrl;
?????、、、
????private?ApiConfig(Builder?builder)?{
????????mInvalidateToken?=?builder.invalidateToken;
????????mBaseUrl?=?builder.baseUrl;
????????、、、
????}
????public?void?init(Context?appContext)?{
????????AppContextUtils.init(appContext);
????}
????public?static?int?getInvalidateToken()?{
????????return?mInvalidateToken;
????}
????public?static?String?getBaseUrl()?{
????????return?mBaseUrl;
????}
????、、、
????public?static?final?class?Builder??{
????????private?int?invalidateToken;
????????private?String?baseUrl;
????????、、、
????????public?Builder?setBaseUrl(String?mBaseUrl)?{
????????????this.baseUrl?=?mBaseUrl;
????????????return?this;
????????}
????????public?Builder?setInvalidateToken(int?invalidateToken)?{
????????????this.invalidateToken?=?invalidateToken;
????????????return?this;
????????}
????????、、、
????????public?ApiConfig?build()?{
????????????return?new?ApiConfig(this);
????????}
????}
}
文章不易,如果大家喜欢这篇文章,或者对你有帮助希望大家多多,点赞,转发,关注?哦。文章会持续更新的。绝对干货!!!
RxJava2+Retrofit2+RxLifecycle3+OkHttp3网络请求封装(动态演示)
标签:ada jdk getc and abstract rac let figure end
原文地址:https://blog.51cto.com/14775360/2485982