![](/img/trans.png)
[英]How do I add an Instance of a class I created to a MultipartEntity?
[英]How do I do MultipartEntity with HttpPost in a background thread?
我該如何執行此代碼,以使其在后台線程中發送?
// TODO: Send image in background
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("userfile", new FileBody(f));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Log.d("Response", "Response: " + s);
}
catch (ClientProtocolException e) {
}
catch (IOException e) {
}
用與您在后台線程中執行任何操作相同的方式-啟動線程來執行此操作。 在Android上,您可以使用AsyncTask
在后台執行內容,並在完成后更新UI。
http://developer.android.com/reference/android/os/AsyncTask.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.