OkHttp中取消呼叫的回叫失败

时间:2015-08-24 21:16:20

标签: java android okhttp

取消OkHttp(版本2.4)客户端中的入队调用有时会抛出Callback failure for canceled call to http://...,并且两个回调方法都不会被执行。

由于我必须向用户提供正面或负面反馈,因此不能接受回调吃掉。我怎样才能解决这个错误?

我创建了带有两个按钮的测试应用程序(一个用于启动下载请求,另一个用于取消它),文本视图和图像视图,大部分时间都显示错误。最初,我在单击“取消”按钮时发现错误,但使用自动点击处理程序对于成功重复错误更为有效。

package com.test.httptest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class MainActivity extends Activity
{
    final private OkHttpClient client = new OkHttpClient();
    private Object cancel = new Object();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onHttpClick(View v)
    {
        TextView tv = (TextView) findViewById(R.id.text_view);
        tv.setText("start");
        downloadImage("http://httpbin.org/image/jpeg");
        // simulate cancel click
        new Handler().postDelayed(new Runnable()
        {
            public void run()
            {
                onCancelClick(null);
            }
        }, 380);
    }

    public void onCancelClick(View v)
    {
        TextView tv = (TextView) findViewById(R.id.text_view);
        tv.setText("canceling");
        client.cancel(cancel);
    }

    public void downloadImage(String url)
    {
        Request request = new Request.Builder()
                .tag(cancel)
                .url(url)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback()
        {
            String result = "";

            @Override
            public void onFailure(Request request, IOException e)
            {
                result = e.getMessage();
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        TextView tv = (TextView) findViewById(R.id.text_view);
                        tv.setText(result);
                    }
                });
            }

            @Override
            public void onResponse(Response response) throws IOException
            {
                if (response.isSuccessful())
                {
                    final byte[] input = response.body().bytes();
                    final int len = (int) response.body().contentLength();

                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            TextView tv = (TextView) findViewById(R.id.text_view);
                            tv.setText("completed");
                            ImageView iv = (ImageView) findViewById(R.id.image_view);
                            Bitmap bm = BitmapFactory.decodeByteArray(input, 0, len);
                            iv.setImageBitmap(bm);
                        }
                    });
                }
                else
                {
                    result = response.body().string();
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            TextView tv = (TextView) findViewById(R.id.text_view);
                            tv.setText(result);
                        }
                    });
                }

            }

        });
    }
}

logcat的:

08-24 23:00:13.151  14403-14451/com.test.httptest I/OkHttpClient﹕ Callback failure for canceled call to http://httpbin.org/...
    java.net.SocketException: Socket closed
            at libcore.io.Posix.recvfromBytes(Native Method)
            at libcore.io.Posix.recvfrom(Posix.java:141)
            at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
            at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
            at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
            at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
            at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
            at okio.Okio$2.read(Okio.java:137)
            at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
            at okio.RealBufferedSource.read(RealBufferedSource.java:50)
            at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
            at okio.Buffer.writeAll(Buffer.java:956)
            at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
            at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:57)
            at hr.artplus.httptestm.MainActivity$2.onResponse(MainActivity.java:87)
            at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:170)
            at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:4)

调用您的onResponse方法。它在堆栈跟踪中显示为

 at hr.artplus.httptestm.MainActivity$2.onResponse(MainActivity.java:87) 

发生的事情是你在这一行得到IOException -

 final byte[] input = response.body().bytes();

可能是因为取消时您在阅读时关闭了流。您的代码允许异常传播回Callback类,该类不会发出另一个回调,因为它已调用onResponse

您可以捕获异常并在回调中处理它 -

if (response.isSuccessful()) {
   try { 
       final byte[] input = response.body().bytes();
       final int len = (int) response.body().contentLength();
   } catch (IOException e) {
       // Signal to the user failure here, re-throw the exception, or 
       // whatever else you want to do on failure
   }
   ...                    
相关问题