2012-11-21 25 views
0

如何执行此代码以便它在后台线程中发送?如何在后台线程中使用HttpPost执行MultipartEntity?

// 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) { 
      } 

回答

相关问题