2011-04-19 89 views
0

我正在将图像文件上传到android中的服务器。 我想知道上传何时完成。是否有任何方法可以知道这一点。 如果在上传完成时自动调用的android中有任何状态。 或如何检查此状态。上传结束时检测

+1

提供一些代码,请。 – 2011-04-19 16:58:35

回答

2

如果您使用HttpClient.execute(),这是一个同步的方法 - 它返回一旦上传完成。

编辑:有没有异步HTTP客户端在Android上,据我所知。对于非阻塞上传,使用HttpClient是工作者线程,并使用Handler.post(Runnable)在主线程中处理完成。

EDIT2:异步HTTP看起来是这样的:

final Handler Ha = new Handler(); //Used to post the results back to the main thread 
new Thread(new Runnable(){ 
    public void run() 
    { 
     HttpPost pr = new HttpPost(MyURL); 
     pr.setEntity(MyUploadData); 
     //More request setup... 
     HttpResponse Resp = new DefaultHttpClient().execute(pr); 
     //Process response here... detect errors, for one thing 
     Ha.post(new Runnable(){ 
      public void run() 
      { 
       //This executes back in the main thread! 
      } 
     }); 
    } 
}).start(); 
+0

那么什么是异步方法,请你解释一下。 – James 2011-04-20 01:23:49

+0

@James:看编辑。 – 2011-04-20 01:28:10

+0

我正在使用HttpClient.execute() – James 2011-04-20 01:43:24