我有两个连续的API调用,所以我决定使用RxJava平面图来完成它,如下代码,
progress.show(this, "Please Wait ...");
HashMap < String, Object > param = new HashMap < > ();
apiClient.getInstance().OPostponed(api_key, param)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function < basicRes, Observable < hodRes >> () {
@Override
public Observable < hodRes > apply(basicRes basicRes) throws Throwable {
HashMap < String, Object > param1 = new HashMap < > ();
return hodClient.getInstance().hodRUpdateStatus("Bearer " + token, param1);
}
}).subscribe(new Observer < hodRes > () {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull hodRes hodRes) {
Log.d(TAG, "onNext: " + hodRes.getData());
progress.dialog.hide();
Toast.makeText(OrderAddressDetails.this, hodRes.getData(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: " + e.getMessage());
progress.dialog.hide();
Toast.makeText(OrderAddressDetails.this, "Please Check Internet Connection !", Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
但我的应用程序总是崩溃,除了以下例外,
io.reactivex.rxjava3.exceptions.CompositeException: 2 exceptions occurred.
at retrofit2.adapter.rxjava3.CallEnqueueObservable$CallCallback.onFailure(CallEnqueueObservable.java:91)
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:175)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:156)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
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:923)
ComposedException 1 :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 10 path $.data
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
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:923)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 10 path $.data
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:401)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:389)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
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:923)
ComposedException 2 :
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:10750)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:2209)
at android.view.View.requestLayout(View.java:27055)
at android.view.View.setFlags(View.java:17525)
at android.view.View.setVisibility(View.java:12466)
at com.android.internal.policy.DecorView.setVisibility(DecorView.java:4737)
at android.app.Dialog.hide(Dialog.java:645)
at com.mtids.b_d_d.OrderAddressDetails$5.onError(OrderAddressDetails.java:161)
at io.reactivex.rxjava3.internal.util.AtomicThrowable.tryTerminateConsumer(AtomicThrowable.java:110)
at io.reactivex.rxjava3.internal.operators.observable.ObservableFlatMap$MergeObserver.checkTerminate(ObservableFlatMap.java:461)
at io.reactivex.rxjava3.internal.operators.observable.ObservableFlatMap$MergeObserver.drainLoop(ObservableFlatMap.java:318)
2021-06-07 15:04:43.748 29162-30184/com.mtids.b_d_d E/AndroidRuntime: at io.reactivex.rxjava3.internal.operators.observable.ObservableFlatMap$MergeObserver.drain(ObservableFlatMap.java:310)
at io.reactivex.rxjava3.internal.operators.observable.ObservableFlatMap$InnerObserver.onError(ObservableFlatMap.java:536)
at retrofit2.adapter.rxjava3.BodyObservable$BodyObserver.onError(BodyObservable.java:77)
at retrofit2.adapter.rxjava3.CallEnqueueObservable$CallCallback.onFailure(CallEnqueueObservable.java:88)
at retrofit2.OkHttpCall$1.callFailure(OkHttpCall.java:175)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:156)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
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:923)
观测器CCD_ 2方法中的CCD_!,我在使用Okhttp日志拦截器的logcat中注意到两个API请求都返回成功响应,这是完美的,但我不知道为什么会出现错误!加上我使用.observeOn(AndroidSchedulers.mainThread())
,那么为什么我会得到Only the original thread that created a view hierarchy can touch its views.
的第二个异常!?我这里缺少什么?
任何帮助都将不胜感激
FlatMap返回一个新的Observable
OPostponed
函数返回observable
上的.observeOn(AndroidSchedulers.mainThread())
,而不是flatMap返回的observable
,所以我想在flatMap 的可观察返回上添加progress.dialog.hide()
0
apiClient.getInstance().OPostponed(api_key, param)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function < basicRes, Observable < hodRes >> () {
@Override
public Observable < hodRes > apply(basicRes basicRes) throws Throwable {
HashMap < String, Object > param1 = new HashMap < > ();
return hodClient.getInstance().hodRUpdateStatus("Bearer " + token, param1);
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer < hodRes > () {
// Rest of your code
}
CalledFromWrongThreadException
,尝试放置
.observeOn(AndroidSchedulers.mainThread())
之后
.flatMap(new Function < basicRes, Observable < hodRes >> () {
上一个答案:
JSON 出现问题
Expected a string but was BEGIN_ARRAY at line 1 column 10 path $.data
basicRes
或hodRes
类都不符合JSON格式。
Btw。你的代码很难阅读。尽量遵守Java惯例。类名应该以大写字母开头。