gpt4 book ai didi

java - okhttp - 拦截器 - 阻止非致命异常记录到 Crashlytics

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:08 24 4
gpt4 key购买 nike

我在 Android 应用程序中使用 Retrofit,这反过来意味着我在使用 OkHttp。我刚刚去了 Alpha 并在我的 crashlytics 报告中看到记录了许多非致命异常。所有这些都源于我的 okhttp 拦截器,然后记录的异常似乎都是在网络不稳定或连接断开等情况下有效的东西。

我怎样才能做到这些异常不会被注销到 crashlytics,从而使我对应用程序中发生的异常的看法变得困惑?


一些异常(exception)的例子:

> Non-fatal Exception: javax.net.ssl.SSLHandshakeException
Connection closed by peer
okhttp3.internal.connection.RealConnection.connectTls (RealConnection.java:281)
okhttp3.internal.connection.RealConnection.establishProtocol (RealConnection.java:251)
okhttp3.internal.connection.RealConnection.connect (RealConnection.java:151)
okhttp3.internal.connection.StreamAllocation.findConnection (StreamAllocation.java:192)
okhttp3.internal.connection.StreamAllocation.findHealthyConnection (StreamAllocation.java:121)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java:100)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:42)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
MY_INTERCEPTOR.intercept (AuthenticationInterceptor.java:30)

> Non-fatal Exception: javax.net.ssl.SSLException
Read error: ssl=0xdee45cc0: I/O error during system call, Software caused connection abort
okio.Okio$2.read (Okio.java:139)
okio.AsyncTimeout$2.read (AsyncTimeout.java:237)
okio.RealBufferedSource.indexOf (RealBufferedSource.java:345)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:217)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:211)
okhttp3.internal.http1.Http1Codec.readResponseHeaders (Http1Codec.java:189)
okhttp3.internal.http.CallServerInterceptor.intercept (CallServerInterceptor.java:75)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:45)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
MY_INTERCEPTOR.intercept (AuthenticationInterceptor.java:30)

这是我的拦截器代码:

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);

if( response.code() == HTTP_AUTHENTICATION_ERROR_CODE ){
Intent intent = new Intent(ACTION_LOGOUT_ACTION);
mContext.sendBroadcast(intent);
}
return response;
}

我知道我可以从“proceed”调用中捕获任何 IOException,但这不会搞乱 OkHttp 对网络错误等的正确处理吗?

最佳答案

当您在代码中显式调用 Crashlytics.logException(Exception)

时,将记录

Non-fatal Exception

请阅读以下信息:

适用于 Android 的

Crashlytics 可让您在应用程序的捕获 block 中记录捕获的异常!要使用此功能,只需将对 Crashlytics.logException(Exception) 的调用添加到您的 catch block :

try {
myMethodThatThrows();
} catch (Exception e) {
Crashlytics.logException(e);
// handle your exception here!
}

所有记录的异常都将在 Crashlytics 仪表板中显示为“非致命”问题。您的问题摘要将包含您用于从崩溃中获取的所有状态信息,以及按 Android 版本和硬件设备分类的故障。

关于java - okhttp - 拦截器 - 阻止非致命异常记录到 Crashlytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47558592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com