克隆一个调用会导致回调被主线程调用

时间:2016-04-04 15:47:57

标签: android retrofit retrofit2

所有通话和回拨都可以。我只是有一个问题,我在回调中使用http错误代码克隆并重试一个调用(使用相同的回调)。

public static abstract class MyCallback<T> implements Callback<T> {
    @Override
    public void onResponse(Call<T> call, retrofit2.Response<T> response) {
        Timber.d("is this the main thread %b", Looper.myLooper() == Looper.getMainLooper());
        if (response.isSuccessful()) {
            //success handling
        } else {
            if (response.code() == 406) {
                // remedy reason for failure
                call.clone().enqueue(MyCallback.this);
            }
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        Timber.e("onFailure %s", t.getMessage());
    }
}

这给出了:

is this the main thread true
is this the main thread false
is this the main thread false
is this the main thread false
is this the main thread false
[looping]

我玩过其他/新的回调,开始新的调用等。只有当我克隆调用时,才会从主线程调用回调。 出现的实际问题是,在成功重试呼叫后我无法更改UI。

使用Retrofit 2.0.1

1 个答案:

答案 0 :(得分:1)

这是改造中的一个错误。杰克沃顿为此创造了一个公关:

https://github.com/square/retrofit/issues/1716