简体   繁体   English

改造+ Okhttp取消操作不起作用

[英]Retrofit + Okhttp cancel operation not working

I am using retrofit in my application like this 我在我的应用程序中使用这样的改造

 final OkHttpClient okHttpClient = new OkHttpClient();
 okHttpClient.interceptors().add(new YourInterceptor());

            final OkClient okClient = new OkClient(okHttpClient);
            Builder restAdapterBuilder = new RestAdapter.Builder();
            restAdapterBuilder.setClient(okClient).setLogLevel(LogLevel.FULL)
                    .setEndpoint("some url");
            final RestAdapter restAdapter = restAdapterBuilder.build();


public class YourInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        // TODO Auto-generated method stub
        Request request = chain.request();

        if (request != null) {
            Request.Builder signedRequestBuilder = request.newBuilder();
            signedRequestBuilder.tag("taggiventorequest");
            request = signedRequestBuilder.build();
            request.tag();
        }
        return chain.proceed(request);
    }
}

after sending request i am calling 发送请求后我打电话

okHttpClient.cancel("taggiventorequest");

but request is not cancelling i am getting the response from retrofit dont know why it is not cancelling my request 但请求没有取消我收到改造的回复不知道为什么它没有取消我的请求

I need volley like cancelation retrofit 我需要排球,如取消改造

As Retrofit API Spec , Canceling request will be included in version 2.0. 作为Retrofit API Spec ,取消请求将包含在2.0版中。

cancel() is a no-op after the response has been received. 收到响应后,cancel()是无操作。 In all other cases the method will set any callbacks to null (thus freeing strong references to the enclosing class if declared anonymously) and render the request object dead. 在所有其他情况下,该方法将任何回调设置为null(因此,如果匿名声明,则释放对封闭类的强引用)并将请求对象渲染为死。 All future interactions with the request object will throw an exception. 将来与请求对象的所有交互都将引发异常。 If the request is waiting in the executor its Future will be cancelled so that it is never invoked. 如果请求在执行程序中等待,则其Future将被取消,以便永远不会调用它。

For now, you can do it by creating custom callback class which implements on Callback from retrofit. 现在,您可以通过创建自定义回调类来实现,该类在回调时通过Callback实现。

public abstract class CancelableCallback<T> implements Callback<T> {

    private boolean canceled;
    private T pendingT;
    private Response pendingResponse;
    private RetrofitError pendingError;

    public CancelableCallback() {
        this.canceled = false;
    }

    public void cancel(boolean remove) {
        canceled = true;
    } 

    @Override
    public void success(T t, Response response) {
        if (canceled) {
            return;
        }
        onSuccess(t, response);
    }

    @Override
    public void failure(RetrofitError error) {
        if (canceled) {
            return;
        }
        onFailure(error);
    }

    protected abstract void onSuccess(T t, Response response);

    protected abstract void onFailure(RetrofitError error);
}

MyApi.java, MyApi.java,

private interface MyApi {
    @GET("/")
    void getStringList(Callback<List<String>> callback);
}

In Activity or Fragment, 在活动或片段中,

RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(Config.URL)
            .build();
MyApi service = restAdapter.create(MyApi.class);
CancelableCallback callback = new CancelableCallback<List<String>>() {
    @Override
    protected void onSuccess(List<String> stringList, Response response) {
        for (String str : stringList) {
            Log.i("Result : ", str);
        }
    }

    @Override
    protected void onFailure(RetrofitError error) {
        Log.e("Error : ", error.getMessage() + "");
    }
};

service.getStringList(callback);

To cancel your request, simple call 要取消您的请求,请致电

callback.cancel();

This is an simple example to cancel each request. 这是取消每个请求的简单示例。 You can handle (cancel, pause, resume) two or more request at the same time by creating callback manager class. 您可以通过创建回调管理器类来同时处理(取消,暂停,恢复)两个或多个请求。 Please take a look that comment for reference. 请看一下评论以供参考。

Hope it will be useful for you. 希望它对你有用。

The problem is that the request is completed, from the docs: 问题是请求已完成,来自文档:

http://square.github.io/okhttp/javadoc/com/squareup/okhttp/OkHttpClient.html#cancel-java.lang.Object- http://square.github.io/okhttp/javadoc/com/squareup/okhttp/OkHttpClient.html#cancel-java.lang.Object-

 cancel 

public OkHttpClient cancel(Object tag) public OkHttpClient cancel(Object tag)

Cancels all scheduled or in-flight calls tagged with tag. 取消标记为tag的所有预定或正在进行的呼叫。 Requests that are already complete cannot be canceled. 已完成的请求无法取消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM