我有一个使用自定义网络拦截器的OkHttp客户端,目的是将令牌插入请求头中。
我一直收到异常
最初的回答:
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.edstart.tazuzu, PID: 32093
java.lang.NullPointerException: interceptor com.edstart.tazuzu.RestAPI.RequestsErrorInterceptor@2bde70f returned null
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:157)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.edstart.tazuzu.RestAPI.TazuzuRequestsInterceptor.intercept(TazuzuRequestsInterceptor.kt:30)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
我尝试过用try/catch包围拦截器,并抛出错误以避免应用程序崩溃,但效果不佳。
拦截器代码:
Original Answer翻译成:"最初的回答"
package com.edstart.tazuzu.RestAPI
import android.util.Log
import com.edstart.tazuzu.Managers.ServerManager
import okhttp3.Interceptor
import okhttp3.Response
import java.net.InetAddress
import kotlin.Exception
/**
* interceptor for the tazuzu api requests to insert the token before the request
* send to the server (for authentication)
*/
class TazuzuRequestsInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain?): Response? {
try {
if (!isInternetAvailable()) {
throw Exception()
} else {
var request = chain?.request()
if (request != null) {
val builder = request.newBuilder()
if (builder != null) {
builder.addHeader("x-access-token", ServerManager.token)
request = builder.build()
if (request != null && chain != null) {
return chain.proceed(request)
}
}
}
}
} catch (e: java.lang.Exception) {
Log.e("###", e.toString())
throw e
}
return null
}
private fun isInternetAvailable(): Boolean {
return try {
val ipAddr = InetAddress.getByName("google.com")
//You can replace it with your name
!ipAddr.equals("")
} catch (e: Exception) {
false
}
}
请帮忙,寻找不会导致应用程序崩溃的异常控制方法。
Original Answer翻译成"最初的回答"
RequestsErrorInterceptor
而不是你发布的TazuzuRequestsInterceptor
。 - laalto