报错信息
2019-05-28 13:39:47.686 21208-21275/com.cxyzy.note E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.cxyzy.note, PID: 21208
java.lang.IllegalStateException: No connection associated with this request; did you use addInterceptor instead of addNetworkInterceptor?
at com.facebook.stetho.okhttp3.StethoInterceptor.intercept(StethoInterceptor.java:71)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.cxyzy.note.network.interceptor.HttpHeaderInterceptor.intercept(HttpHeaderInterceptor.kt:22)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.cxyzy.note.network.interceptor.HttpLogInterceptor.intercept(HttpLogInterceptor.kt:43)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
代码
- 依赖
debugImplementation 'com.facebook.stetho:stetho:1.5.1'
debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
releaseImplementation "net.igenius:stetho-no-op:1.1"
- Application启动stetho
if (BuildConfig.DEBUG) {
Stetho.initialize(
newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build())
}
- 设置okhttp过滤器
if (BuildConfig.DEBUG) {
addInterceptor(StethoInterceptor())
}
问题原因
应该使用addNetworkInterceptor,而不是addInterceptor. 修改okhttp过滤器设置代码
if (BuildConfig.DEBUG) {
addNetworkInterceptor(StethoInterceptor())
}