我有一个AsyncFacebookRunner可将图像上传到Facebook。我想创建一个取消按钮,将停止下载。

一个人怎么能做到这一点?

编辑:

这是我的用法:

byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", params, "POST",
new PhotoUploadListener(dialog), null);

最佳答案

您始终可以扩展AsyncFacebookRunner类并覆盖request方法。
像这样:

public class CancelableAsyncFacebookRunner extends AsyncFacebookRunner {
    private Thread requestThread;

    public AsyncFacebookRunner(Facebook fb) {
        super(fb);
    }

    @Override
    public void request(final String graphPath,
            final Bundle parameters,
            final String httpMethod,
            final RequestListener listener,
            final Object state) {

        this.requestThread = new Thread() {
            @Override
            public void run() {
                try {
                    String resp = fb.request(graphPath, parameters, httpMethod);
                    listener.onComplete(resp, state);
                } catch (FileNotFoundException e) {
                    listener.onFileNotFoundException(e, state);
                } catch (MalformedURLException e) {
                    listener.onMalformedURLException(e, state);
                } catch (IOException e) {
                    listener.onIOException(e, state);
                }
            }
        };
    }

    public void cancel() {
        this.requestThread.interrupt();
    }
}


它尚未经过测试,但应该可以为您提供总体思路。



编辑

现在考虑一下,这已经没有意义了,因为您想使用AsyncFacebookRunner发出多个请求,而cancel仅会取消最后一个请求。

我建议返回该线程,然后可以在其他地方进行中断,但是您不能像这样更改方法的签名,并且创建新方法将无法使用在中定义的其他request方法AsyncFacebookRunner类。

相反,您可以执行以下操作:

public class CancelableAsyncFacebookRunner extends AsyncFacebookRunner {
    private Hashtable<String, Thread> requestThreads;

    public AsyncFacebookRunner(Facebook fb) {
        super(fb);
        this.requestThreads = new Hashtable<String, Thread>();
    }

    @Override
    public void request(final String id,
            final String graphPath,
            final Bundle parameters,
            final String httpMethod,
            final RequestListener listener,
            final Object state) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    String resp = fb.request(graphPath, parameters, httpMethod);
                    requestThreads.remove(id);
                    listener.onComplete(resp, state);
                } catch (FileNotFoundException e) {
                    requestThreads.remove(id);
                    listener.onFileNotFoundException(e, state);
                } catch (MalformedURLException e) {
                    requestThreads.remove(id);
                    listener.onMalformedURLException(e, state);
                } catch (IOException e) {
                    requestThreads.remove(id);
                    listener.onIOException(e, state);
                }
            }
        });

        this.requestThreads.put(id, thread);
        thread.start();
    }

    public void cancel(String id) {
        if (this.requestThreads.containsKey(id) {
            this.requestThreads.get(id).interrupt();
        }
    }
}


您需要以某种方式为请求生成一个ID,可以很简单,例如:

String.valueOf(System.currentTimeMillis());

09-30 22:59