Android-OkHttp,-一行代码-OkHttp提升请求稳定性,金九银十正确打开方式

本文探讨了如何使用Interceptor处理OkHttp中的异常,避免因NullPointerException等非IOException导致的应用崩溃,提供了一种增加请求稳定性的方法。通过创建SafeGuardInterceptor,将所有异常转化为IOException,并在OkHttpClient中添加此拦截器,可以确保网络请求失败不会直接导致应用崩溃。
摘要由CSDN通过智能技术生成

等等,我记得OkHttp有处理异常的情况呢。

嗯,确实,OkHttp有处理异常的情况,比如发生异常会调用onFailure。比如下面的Callback的内容介绍。

interface Callback {
/**

  • Called when the request could not be executed due to cancellation, a connectivity problem or
  • timeout. Because networks can fail during an exchange, it is possible that the remote server
  • accepted the request before the failure.
    */
    fun onFailure(call: Call, e: IOException)

/**

  • Called when the HTTP response was successfully returned by the remote server. The callback may
  • proceed to read the response body with [Response.body]. The response is still live until its
  • response body is [closed][ResponseBody]. The recipient of the callback may consume the r

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

esponse

  • body on another thread.
  • Note that transport-layer success (receiving a HTTP response code, headers and body) does not
  • necessarily indicate application-layer success: response may still indicate an unhappy HTTP
  • response code like 404 or 500.
    */
    @Throws(IOException::class)
    fun onResponse(call: Call, response: Response)
    }

是的.

  • OkHttp只处理了IOException的情况,
  • NullPointerException不是IOException的子类

所以没有被处理,发生了崩溃。

那么有没有办法解决,让这种崩溃不发生,对用户不进行干扰呢?其实是可以的。

三.使用Interceptor

package com.example.okhttpexceptionsample

import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**

  • 对于Interceptor的intercept中可能出现的Throwable包裹成IOExceptionWrapper,转成网络请求失败,而不是应用崩溃
    */
    class SafeGuardInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
    try {
    return chain.proceed(chain.request())
    } catch (t: Throwable) {
    throw IOExceptionWrapper(“SafeGuarded when requesting ${chain.request().url}”, t)
    }
    }
    }

/**

  • 将chain.proceed处理中发生的Throwable包装成IOExceptionWrapper
    */
    class IOExceptionWrapper(message: String?, cause: Throwable?) : IOException(message, cause)

上面的代码,我们将任何Throwable的转成IOExceptionWrapper(伪装成IOException),然后添加到OkHttpClient中

fun createOKHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(SafeGuardInterceptor())
.build()
}

当我们再次执行有NPE的代码,日志就发生了改变(不再是崩溃的日志,而是异常的日志)

W System.err: com.example.okhttpexceptionsample.IOExceptionWrapper: SafeGuarded=blablabla
W System.err: at com.example.okhttpexceptionsample.SafeGuardInterceptor.intercept(SafeGuardInterceptor.kt:12)
W System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
W System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
W System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
W System.err: at okhttp3.RealCall A s y n c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值