在我們使用Retrofit 1.9時,我的同事創(chuàng)建了以下課程
public class SomeApiCallAction {
private Subscription subscription;
private NoInternetConnectionInterface noInternetConnectionInterface;
public interface NoInternetConnectionInterface {
PublishSubject<Integer> noInternetConnection(Throwable throwable);
}
public void execute(Subscriber subscriber, NoInternetConnectionInterface noInternetConnectionInterface) {
this.noInternetConnectionInterface = noInternetConnectionInterface;
this.subscription = retrofit.someService().someApiCall()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber)
.retryWhen(retryFunction);
}
public void cancel() {
if (this.subscription != null) {
this.subscription.unsubscribe();
}
}
private Func1<Observable<? extends Throwable>, Observable<?>> retryFunction = new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
return observable.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(final Throwable throwable) {
if (noInternetConnectionInterface!= null && (throwable instanceof IOException || throwable instanceof SocketTimeoutException)) {
return noInternetConnectionInterface.noInternetConnection(throwable);
}else{
return Observable.error(throwable);
}
}
});
}
}
SomeApiCallAction只是一個簡單的類,它包含了內(nèi)部的改進(jìn)api調(diào)用,唯一特別的是它的重試功能.重試函數(shù)將檢查throwable是否是IOException或SocketTimeoutException,如果是,它將調(diào)用接口,以便我們可以向用戶提供重試對話框,詢問他們是否要重試該操作.我們的用法類似于以下代碼段
public class SomeActivity implement NoInternetConnectionInterface {
@OnClick(R.id.button)
public void do(View v) {
new SomeApiCallAction().execute(
new Subscriber(),
this
)
}
@Override
public PublishSubject<Integer> noInternetConnection(final Throwable throwable) {
Log.i("Dev", Thread.currentThread() " Error!");
final PublishSubject<Integer> subject = PublishSubject.create();
runOnUiThread(new Runnable() {
@Override
public void run() {
NoInternetDialogFragment dialog = NoInternetDialogFragment.newInstance();
dialog.setNoInternetDialogFragmentListener(new NoInternetDialogFragmentListener{
@Override
public void onUserChoice(boolean retry, NoInternetDialogFragment dialog) {
Log.i("Dev", Thread.currentThread() " Button Click!");
if (retry) {
subject.onNext(1);
} else {
subject.onError(throwable);
}
dialog.dismiss();
}
});
dialog.show(getSupportFragmentManager(), NoInternetDialogFragment.TAG);
}
});
return subject;
}
}
當(dāng)我們使用Retrofit 1.9.0時,這個實現(xiàn)工作得非常完美.我們通過打開飛行模式進(jìn)行測試,然后按下按鈕執(zhí)行api呼叫.
>第一次執(zhí)行失敗,我在重試功能中得到了UnknownHostException. >所以,我調(diào)用界面(Activity)來顯示重試對話框 >我仍然在飛行模式下按重試按鈕重復(fù)執(zhí)行 >正如預(yù)期的那樣,用戶按下重試按鈕后發(fā)生的每次執(zhí)行都失敗了,我總是在重試功能中得到UnknownHostException. >如果我一直按下重試按鈕,重試對話框?qū)⒂肋h(yuǎn)顯示,直到我關(guān)閉飛行模式.
但在我們更新我們的依賴關(guān)系之后
'com.squareup.retrofit2:retrofit:2.0.2'
'com.squareup.retrofit2:adapter-rxjava:2.0.2'
我們再試一次,但這次行為改變了,
>第一次執(zhí)行失敗,我在重試功能中獲得了與之前相同的UnknownHostException. >所以,我調(diào)用界面(Activity)來顯示重試對話框 >我仍然在飛行模式下按重試按鈕重復(fù)執(zhí)行 >但是這一次,在重試函數(shù)中,我得到了NetworkOnMainThreadException而不是像它那樣接收UnknowHostException. >因此條件不匹配,接口沒有被調(diào)用,結(jié)果只有1個重試對話框呈現(xiàn)給用戶.
以下是上面代碼的日志
Thread[android_0,5,main] Error!
Thread[main,5,main] Button Click!
你知道這會導(dǎo)致什么嗎?任何建議,評論都會非常感激.
注意:以下是我們一直在使用并可能相關(guān)的其他依賴項.但是它們最近沒有更新,從本項目開始就使用這些版本.
'com.jakewharton:butterknife:8.0.1'
'io.reactivex:rxandroid:1.1.0'
'io.reactivex:rxjava:1.1.0'
'com.google.dagger:dagger-compiler:2.0'
'com.google.dagger:dagger:2.0'
'javax.annotation:jsr250-api:1.0'
更多信息
我只是將我的代碼重置回到我們使用Retrofit 1.9的時候,我發(fā)現(xiàn)打印日志不同
Thread[Retrofit-Idle,5,main] Error!
Thread[main,5,main] Button Click!
不確定這是否與問題有關(guān),但很明顯,在1.9.0中,我將不同線程中的接口調(diào)用為2.0.0
最終編輯
在閱讀了@JohnWowUs的答案并按照他提供的鏈接后,我發(fā)現(xiàn)在Retrofit 2中,網(wǎng)絡(luò)調(diào)用默認(rèn)是同步的
要解決我的問題,有兩種方法可以解決此問題
1.)按照@JohnWowUs的建議,通過為retryFunction指定線程
this.subscription = retrofit.someService().someApiCall()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber)
.retryWhen(retryFunction, Schedulers.io());
2.)創(chuàng)建改造對象時,在創(chuàng)建RxJavaCallAdapterFactory時指定線程
retrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(getGson()))
.addCallAdapterFactory(
RxJavaCallAdapterFactory.createWithScheduler(
Schedulers.from(threadExecutor)
)
)
.build();
解決方法: 我認(rèn)為問題在于,當(dāng)您重新訂閱時,由于在retryWhen中使用默認(rèn)的trampoline調(diào)度程序而在主線程上訂閱. Retrofit 1.9為您處理了調(diào)度,因此使用subscribeOn毫無意義.問題討論是here.在Retrofit 2中我相信這已經(jīng)改變了所以你應(yīng)該嘗試類似的東西
this.subscription = retrofit.someService().someApiCall()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber)
.retryWhen(retryFunction, Schedulers.io());
來源:https://www./content-4-275151.html
|