简体   繁体   中英

Multiple Httppost connections?

How can I make multiple Httppost connections at the same time? I keep getting the following error:

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

My httppost code

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);

i have used like this and it is working.

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;
}     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM