2014-05-07 78 views
0

如何在同一時間創建多個Httppost連接?我不斷收到以下錯誤:多個Httppost連接?

05-05 19:28:08.920: E/AndroidRuntime(6983): Make sure to release the connection before allocating another one.

我httppost代碼

HttpPost httpPost = new HttpPost("http://mydomain.com/api"); 
     MultipartEntity mentity = new MultipartEntity(); 
     mentity.addPart("token",token); 
     mentity.addPart("ts",ts);       
     httpPost.setEntity(mentity); 
     response = httpclient.execute(httpPost); 
     HttpEntity httpEntity = response.getEntity(); 
     body = EntityUtils.toString(httpEntity);    
     EntityUtils.consume(httpEntity); 
     EntityUtils.consume(entity); 
+0

發佈您正在使用的代碼 – panini

回答

0

我用這樣的,它是工作。

public String reportCrime(String uploadFile, int userid, int crimetype, 
     String crimedetails, String lat, String longi, String reporteddate) { 
    String url; 
    MultipartEntity entity; 
    try { 
     url = String.format(Constant.SERVER_URL 
       + "push_notification/reportCrime.php"); 

     entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     // 
     File file = new File(uploadFile); 
     if (!file.equals("Image not Provided.")) { 
      if (file.exists()) { 

       Bitmap bmp = BitmapFactory.decodeFile(uploadFile); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       bmp.compress(CompressFormat.JPEG, 70, bos); 
       InputStream in = new ByteArrayInputStream(bos.toByteArray()); 
       ContentBody foto = new InputStreamBody(in, "image/jpeg", uploadFile); 



       entity.addPart("image", foto); 
      } 
     } else { 
      FormBodyPart image = new FormBodyPart("image", new StringBody(
        "")); 
      entity.addPart(image); 
     } 

     FormBodyPart userId = new FormBodyPart("userId", new StringBody(
       String.valueOf(userid))); 
     entity.addPart(userId); 

     FormBodyPart crimeType = new FormBodyPart("crimetype", 
       new StringBody(String.valueOf(crimetype))); 
     entity.addPart(crimeType); 

     FormBodyPart crimeDetails = new FormBodyPart("crimedetail", 
       new StringBody(crimedetails)); 
     entity.addPart(crimeDetails); 

     FormBodyPart latittude = new FormBodyPart("latittude", 
       new StringBody(lat)); 
     entity.addPart(latittude); 

     FormBodyPart longitude = new FormBodyPart("longitude", 
       new StringBody(longi)); 
     entity.addPart(longitude); 

     FormBodyPart reportedDate = new FormBodyPart("reporteddatetime", 
       new StringBody(reporteddate)); 
     entity.addPart(reportedDate); 

    } catch (UnsupportedEncodingException e1) { 
     e1.printStackTrace(); 
     return "error"; 
    } 

    HttpParams httpParams = new BasicHttpParams(); 

    HttpContext httpContext = new BasicHttpContext(); 
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000); 
    HttpConnectionParams.setSoTimeout(httpParams, 10000); 

    try { 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(entity); 
     client = new DefaultHttpClient(); 
     HttpResponse response = client.execute(httpPost); 
     BufferedReader in = null; 
     try { 
      in = new BufferedReader(new InputStreamReader(response 
        .getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(); 
      String line = null; 
      String NL = System.getProperty("line.separator"); 
      while ((line = in.readLine()) != null) { 
       sb.append(line + NL); 
      } 

      result = sb.toString(); 
     } finally { 
      if (in != null) 
       in.close(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return result; 
}