Android Http Get executeHttpPost(String url, ArrayList postParameters)

Here you can find the source of executeHttpPost(String url, ArrayList postParameters)

Description

Performs an HTTP Post request to the specified url with the specified parameters, and return the string from the HttpResponse.

Parameter

Parameter Description
url The web address to post the request to
postParameters The parameters to send via the request

Exception

Parameter Description
Exception an exception

Return

The result string of the request

Declaration

public static String executeHttpPost(String url,
        ArrayList<NameValuePair> postParameters) throws Exception 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class Main {
    /** The time it takes for our client to timeout */
    public static final int HTTP_TIMEOUT = 10 * 1000;
    /** Single instance of our HttpClient */
    private static HttpClient httpClient;

    /**/*from  w w  w  .ja  v  a  2s .  com*/
     * Performs an HTTP Post request to the specified url with the
     * specified parameters, and return the string from the HttpResponse.
     *
     * @param url The web address to post the request to
     * @param postParameters The parameters to send via the request
     * @return The result string of the request
     * @throws Exception
     */
    public static String executeHttpPost(String url,
            ArrayList<NameValuePair> postParameters) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpPost request = new HttpPost(url);
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                    postParameters);
            request.setEntity(formEntity);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String result = sb.toString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Get singleton of HttpClient object.
     *
     * @return an HttpClient object with connection parameters set
     */
    private static HttpClient getHttpClient() {
        if (httpClient == null) {
            httpClient = new DefaultHttpClient();
            final HttpParams params = httpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
        }
        return httpClient;
    }
}

Related

  1. request(String url)
  2. requestGet(String url)
  3. executeHttpGet(String url)
  4. getHTTP(String... params)
  5. handleURLEncodedResponse(HttpResponse response)
  6. handleXMLResponse(HttpResponse response)
  7. getStringFromConnection( HttpURLConnection connection)