【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第1页
【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第2页
【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第3页
【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第4页
【移动应用开发技术】Android OkHttp 一行代码 OkHttp提升请求稳定性_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论