gpt4 book ai didi

retrofit - NetworkOnMainThread RxJava + 改造 + Lollipop+

转载 作者:行者123 更新时间:2023-12-04 08:05:44 26 4
gpt4 key购买 nike

我在运行 https 时收到有关 Lollipop+ 上 NetworkOnMainThread 异常的报告api 调用使用 RetrofitRxAndroid .

我已经隔离了代码并创建了以下仍然失败的示例。

这是build.gradle :

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "com.example.bug"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'

compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'io.reactivex:rxandroid:1.0.1'
}

这是唯一的事件:
public class HomeActivity extends Activity {

private static final String URL_BASE = "https://some.https.api.com/";
private static final String ENDPOINT = "some/endpoint";

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL_BASE)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

MyApi mService = retrofit.create(MyApi.class);

setContentView(R.layout.home_activity);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(v->
mService.whatever(new ParamObject())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
response -> Log.d("BUG", response.status),
error -> Log.d("BUG", error.toString()))
);
}

interface MyApi {

@POST(ENDPOINT)
Observable<ResponseObject> whatever( @Body ParamObject requirements);
}

class ResponseObject {
public String status;
}

class ParamObject {
}
}

这是异常堆栈跟踪:
E/AndroidRuntime(28345): FATAL EXCEPTION: main
E/AndroidRuntime(28345): Process: com.example.bug, PID: 28345
E/AndroidRuntime(28345): java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
E/AndroidRuntime(28345): at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
E/AndroidRuntime(28345): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(28345): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(28345): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(28345): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime(28345): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(28345): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(28345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime(28345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime(28345): Caused by: android.os.NetworkOnMainThreadException
E/AndroidRuntime(28345): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
E/AndroidRuntime(28345): at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:724)
E/AndroidRuntime(28345): at okio.Okio$1.write(Okio.java:80)
E/AndroidRuntime(28345): at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
E/AndroidRuntime(28345): at okio.RealBufferedSink.flush(RealBufferedSink.java:221)
E/AndroidRuntime(28345): at com.squareup.okhttp.internal.framed.Http2$Writer.rstStream(Http2.java:475)
E/AndroidRuntime(28345): at com.squareup.okhttp.internal.framed.FramedConnection.writeSynReset(FramedConnection.java:356)
E/AndroidRuntime(28345): at com.squareup.okhttp.internal.framed.FramedStream.close(FramedStream.java:222)
E/AndroidRuntime(28345): at com.squareup.okhttp.internal.http.FramedTransport.disconnect(FramedTransport.java:215)
E/AndroidRuntime(28345): at com.squareup.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java:573)
E/AndroidRuntime(28345): at com.squareup.okhttp.Call.cancel(Call.java:122)
E/AndroidRuntime(28345): at retrofit.OkHttpCall.cancel(OkHttpCall.java:162)
E/AndroidRuntime(28345): at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe$1.call(RxJavaCallAdapterFactory.java:102)
E/AndroidRuntime(28345): at rx.subscriptions.BooleanSubscription.unsubscribe(BooleanSubscription.java:72)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
E/AndroidRuntime(28345): at rx.Subscriber.unsubscribe(Subscriber.java:98)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
E/AndroidRuntime(28345): at rx.Subscriber.unsubscribe(Subscriber.java:98)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
E/AndroidRuntime(28345): at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
E/AndroidRuntime(28345): at rx.Subscriber.unsubscribe(Subscriber.java:98)
E/AndroidRuntime(28345): at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:90)
E/AndroidRuntime(28345): at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:201)
E/AndroidRuntime(28345): at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:170)
E/AndroidRuntime(28345): at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
E/AndroidRuntime(28345): ... 8 more

一些东西:
  • 此代码示例在 Android 4.x
  • 中运行良好
  • 我已经针对 GitHub API 尝试了这个示例(稍作改动),它工作得很好。
  • 该 API 在 Google 的应用引擎上运行。
  • 请注意,observable 正在 Schedulers.io() 上订阅。并在 AndroidSchedulers.mainThread() 上观察到

  • 有任何想法吗?

    编辑
  • 在仔细查看堆栈跟踪后,我认为有一些关于 http2 的内容。
    虽然报告的问题不一样here ,下面一行来限制OkHttp可用的协议(protocol)使它再次工作。
    okHttpClient.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
  • 最佳答案

    OkHttp 中的一个已知错误是取消调用会在取消线程上执行 I/O。将在 future 的版本中修复。

    关于retrofit - NetworkOnMainThread RxJava + 改造 + Lollipop+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266886/

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