gpt4 book ai didi

android - 一般如何停止 AsyncFacebookRunner 或停止请求?

转载 作者:行者123 更新时间:2023-11-30 04:10:11 25 4
gpt4 key购买 nike

我有一个 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 只会取消最后一个请求。

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

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

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());

关于android - 一般如何停止 AsyncFacebookRunner 或停止请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com