![【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第1页](http://file4.renrendoc.com/view/2c233d2839d244d95cc3211d627cbf7a/2c233d2839d244d95cc3211d627cbf7a1.gif)
![【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第2页](http://file4.renrendoc.com/view/2c233d2839d244d95cc3211d627cbf7a/2c233d2839d244d95cc3211d627cbf7a2.gif)
![【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第3页](http://file4.renrendoc.com/view/2c233d2839d244d95cc3211d627cbf7a/2c233d2839d244d95cc3211d627cbf7a3.gif)
![【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第4页](http://file4.renrendoc.com/view/2c233d2839d244d95cc3211d627cbf7a/2c233d2839d244d95cc3211d627cbf7a4.gif)
![【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第5页](http://file4.renrendoc.com/view/2c233d2839d244d95cc3211d627cbf7a/2c233d2839d244d95cc3211d627cbf7a5.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】AndroidOkHttp,一行代码OkHttp提升请求稳定性
OkHttp是可以说是Android开发中,每个项目都必需依赖的网络库,我们可以很便捷高效的处理网络请求,极大的提升了编码效率。但是有时候,我们使用OkHttp也会遇到这样的问题
一.崩溃的stacktrace
EAndroidRuntime:FATALEXCEPTION:OkHttpDispatcher
EAndroidRuntime:Process:com.example.okhttpexceptionsample,PID:13564
EAndroidRuntime:java.lang.NullPointerException:blablabla
EAndroidRuntime:atcom.example.okhttpexceptionsample.MainActivity$createNPEInterceptor$1.intercept(MainActivity.kt:61)
EAndroidRuntime:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:112)
EAndroidRuntime:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:87)
EAndroidRuntime:atokhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
EAndroidRuntime:atokhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
EAndroidRuntime:atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
EAndroidRuntime:atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
EAndroidRuntime:atjava.lang.Thread.run(Thread.java:784)
二.为什么会崩溃
从上面的stacktrace,我们可以分析到,发生了NullPointerException。发生了崩溃。
OkHttp是可以说是Android开发中,每个项目都必需依赖的网络库,我们可以很便捷高效的处理网络请求,极大的提升了编码效率。但是有时候,我们使用OkHttp也会遇到这样的问题从上面的stacktrace,我们可以分析到,发生了NullPointerException。发生了崩溃。等等,我记得OkHttp有处理异常的情况呢。嗯,确实,OkHttp有处理异常的情况,比如发生异常会调用onFailure。比如下面的Callback的内容介绍。interfaceCallback{
/**
*Calledwhentherequestcouldnotbeexecutedduetocancellation,aconnectivityproblemor
*timeout.Becausenetworkscanfailduringanexchange,itispossiblethattheremoteserver
*acceptedtherequestbeforethefailure.
*/
funonFailure(call:Call,e:IOException)
/**
*CalledwhentheHTTPresponsewassuccessfullyreturnedbytheremoteserver.Thecallbackmay
*proceedtoreadtheresponsebodywith[Response.body].Theresponseisstillliveuntilits
*responsebodyis[closed][ResponseBody].Therecipientofthecallbackmayconsumetheresponse
*bodyonanotherthread.
*
*Notethattransport-layersuccess(receivingaHTTPresponsecode,headersandbody)doesnot
*necessarilyindicateapplication-layersuccess:`response`maystillindicateanunhappyHTTP
*responsecodelike404or500.
*/
@Throws(IOException::class)
funonResponse(call:Call,response:Response)
}是的.所以没有被处理,发生了崩溃。那么有没有办法解决,让这种崩溃不发生,对用户不进行干扰呢?其实是可以的。三.使用Interceptorpackagecom.example.okhttpexceptionsample
importokhttp3.Interceptor
importokhttp3.Response
importjava.io.IOException
/**
*对于Interceptor的intercept中可能出现的Throwable包裹成IOExceptionWrapper,转成网络请求失败,而不是应用崩溃
*/
classSafeGuardInterceptor:Interceptor{
overridefunintercept(chain:Interceptor.Chain):Response{
try{
returnceed(chain.request())
}catch(t:Throwable){
throwIOExceptionWrapper("SafeGuardedwhenrequesting${chain.request().url}",t)
}
}
}
/**
*将ceed处理中发生的Throwable包装成IOExceptionWrapper
*/
classIOExceptionWrapper(message:String?,cause:Throwable?):IOException(message,cause)上面的代码,我们将任何Throwable的转成IOExceptionWrapper(伪装成IOException),然后添加到OkHttpClient中funcreateOKHttpClient():OkHttpClient{
returnOkHttpClient.Builder()
.addInterceptor(SafeGuardInterceptor())
.build()
}当我们再次执行有NPE的代码,日志就发生了改变(不再是崩溃的日志,而是异常的日志)WSystem.err:com.example.okhttpexceptionsample.IOExceptionWrapper:SafeGuarded=blablabla
WSystem.err:atcom.example.okhttpexceptionsample.SafeGuardIercept(SafeGuardInterceptor.kt:12)
WSystem.err:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:112)
WSystem.err:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:87)
WSystem.err:atokhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
WSystem.err:atokhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
WSystem.err:atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
WSystem.err:atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
WSystem.err:atjava.lang.Thread.run(Thread.java:784)
WSystem.err:Causedby:java.lang.NullPointerException:blablabla
WSystem.err:atcom.example.okhttpexceptionsample.MainActivity$createNPEInterceptor$1.intercept(MainActivity.kt:61)
WSystem.err:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:112)
WSystem.err:aternal.http.RealInterceptorCceed(RealInterceptorChain.kt:87)
WSystem.err:atcom.example.okhttpexceptionsample.SafeGuardIercept(SafeGuardInterceptor.kt:10)
WSystem.err:.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 房屋建筑工程施工承包合同范本(2024版)
- 【正版授权】 ISO 8662-14:1996 EN Hand-held portable power tools - Measurement of vibrations at the handle - Part 14: Stone-working tools and needle scalers
- 【正版授权】 ISO 8637-1:2024 EN Extracorporeal systems for blood purification - Part 1: Haemodialysers,haemodiafilters,haemofilters and haemoconcentrators
- 2024-2030全球及中国智能盘点机器人行业竞争调研及投资前景建议研究报告
- 单位代收款委托书范本
- 房产交房代表委托书
- 解除施工合同法律
- 地下综合管廊监控系统开槽协议
- 建筑材料购销协议
- 公司设立过程中的委托书办理详解
- PE管水压试验记录表
- 2021年摇床重选安全操作规程
- 工程测量控制点交桩记录表(共6页)
- 工学结合一体化课程开发与实践精品PPT课件
- 铁一院线路设计软件说明
- 申请认定教师资格体检表
- 模具移交协议及清单
- 陶艺小房子の制作(课堂PPT)
- 血液净化中心各项规章制度
- 抗菌药物临床应用监测技术方案
- 3 篇 中学、小学“双减”工作典型案例典型做法和双减经验交流总结 (精选合辑)
评论
0/150
提交评论