我正在將圖像文件上傳到android中的服務器。 我想知道上傳何時完成。是否有任何方法可以知道這一點。 如果在上傳完成時自動調用的android中有任何狀態。 或如何檢查此狀態。上傳結束時檢測
Q
上傳結束時檢測
0
A
回答
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();
提供一些代碼,請。 – 2011-04-19 16:58:35