问题描述
我有一个 AsyncFacebookRunner
的上传至Facebook的图像。我想创建一个取消按钮,将停止下载。
如何才能做到这一点?
编辑:
这是我如何使用它:
字节[]数据= NULL;
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
数据= baos.toByteArray();
捆绑PARAMS =新包();
params.putByteArray(图片,数据);
AsyncFacebookRunner mAsyncRunner =新AsyncFacebookRunner(脸谱);
mAsyncRunner.request(ME /照片,则params,POST,
新PhotoUploadListener(对话),NULL);
您可以随时扩展 AsyncFacebookRunner
类和重写要求
方法。
事情是这样的:
公共类CancelableAsyncFacebookRunner扩展AsyncFacebookRunner {
私人螺纹requestThread; 公共AsyncFacebookRunner(Facebook的FB){
超(FB);
} @覆盖
公共无效请求(最后弦乐graphPath,
最后的捆绑参数,
最后弦乐列举HTTPMethod,
最后RequestListener监听器,
最终对象的状态){ this.requestThread =新主题(){
@覆盖
公共无效的run(){
尝试{
字符串RESP = fb.request(graphPath,参数,列举HTTPMethod);
listener.onComplete(RESP,状态);
}赶上(FileNotFoundException异常五){
listener.onFileNotFoundException(即状态);
}赶上(MalformedURLException的E){
listener.onMalformedURLException(即状态);
}赶上(IOException异常五){
listener.onIOException(即状态);
}
}
};
} 公共无效取消(){
this.requestThread.interrupt();
}
}
这尚未经过测试,但应该给你的总体思路。
修改
现在我想想,这没有什么意义,因为你要使用的 AsyncFacebookRunner
进行多次请求和取消
将取消只有最后的请求。
我会建议返回线程,然后不得不interupt别的地方的能力,但你不能改变这样的方法的签名,并创建一个新的方法不会使它可以使用其他<$ C在 AsyncFacebookRunner
类中定义$ C>要求方法。
相反,你可以这样做:
公共类CancelableAsyncFacebookRunner扩展AsyncFacebookRunner {
私人Hashtable的&LT;弦乐,螺纹&GT; requestThreads; 公共AsyncFacebookRunner(Facebook的FB){
超(FB);
this.requestThreads =新的Hashtable&LT;弦乐,螺纹&GT;();
} @覆盖
公共无效请求(最后字符串的ID,
最后弦乐graphPath,
最后的捆绑参数,
最后弦乐列举HTTPMethod,
最后RequestListener监听器,
最终对象的状态){
线程线程=新主题(){
@覆盖
公共无效的run(){
尝试{
字符串RESP = fb.request(graphPath,参数,列举HTTPMethod);
requestThreads.remove(ID);
listener.onComplete(RESP,状态);
}赶上(FileNotFoundException异常五){
requestThreads.remove(ID);
listener.onFileNotFoundException(即状态);
}赶上(MalformedURLException的E){
requestThreads.remove(ID);
listener.onMalformedURLException(即状态);
}赶上(IOException异常五){
requestThreads.remove(ID);
listener.onIOException(即状态);
}
}
}); this.requestThreads.put(ID,螺纹);
thread.start();
} 公共无效取消(字符串ID){
如果(this.requestThreads.containsKey(ID){
this.requestThreads.get(ID).interrupt();
}
}
}
您将需要以某种方式生成一个ID的请求,可以简单的东西,如:
将String.valueOf(System.currentTimeMillis的());
I have an AsyncFacebookRunner
that uploads an image to Facebook. I want to create a cancel button that will stop the download.
How can one achieve that?
EDIT:
This is how I use it:
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);
You can always extend the AsyncFacebookRunner
class and override the request
method.
Something like this:
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();
}
}
It hasn't been tested, but should give you the general idea.
Edit
Now that I think about it, this makes little sense, since you want to use the AsyncFacebookRunner
to make multiple requests and the cancel
will cancel the last request only.
I would suggest returning the thread and then have the ability to interupt it somewhere else, but you can't change the signature of the method like this and creating a new method won't make it possible to use other request
methods defined in the AsyncFacebookRunner
class.
Instead you can do something like:
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();
}
}
}
You'll need to generate an id somehow for the request, can be something simple like:
String.valueOf(System.currentTimeMillis());
这篇关于如何停止AsyncFacebookRunner或停止一般的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!