get Url By Post : Url « Network « Android

get Url By Post

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

class Main {
  private static HttpParams defaultHttpParams = new BasicHttpParams();
  private static final String UTF8 = "UTF-8";

  public static String getUrlByPost(String url, Map<String, String> params, Map<String, String> headers,
      int maxRetries) throws IOException {
    String result = null;
    int retries = 0;
    DefaultHttpClient httpclient = new DefaultHttpClient(defaultHttpParams);

    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    if (params != null) {
      Set<Entry<String, String>> paramsSet = params.entrySet();
      for (Entry<String, String> entry : paramsSet) {
        formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

    UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formParams, UTF8);
    HttpPost httppost = new HttpPost(url);

    if (headers != null) {
      Set<Entry<String, String>> headersSet = headers.entrySet();
      for (Entry<String, String> entry : headersSet) {
        httppost.setHeader(entry.getKey(), entry.getValue());

    while (retries < maxRetries && result == null) {
      try {
        HttpEntity responseEntity = httpclient.execute(httppost).getEntity();
        if (responseEntity != null) {
          result = EntityUtils.toString(responseEntity).trim();
      } catch (SocketException se) {
        if (retries > maxRetries) {
          throw se;
        } else {
        //  Log.v(TAG, "SocketException, retrying " + retries, se);

    return result;


Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Read from a URL
14.Pull the raw text content of the given URL.
15.Get Video from URL
16.Gets data from URL
17.get Url Response
18.URL Encode Utils
19.Downloads a file given URL to specified destination
20.get Host From Url
21.encode Url
22.decode Url
23.Convert a byte array to a URL encoded string
24.get Url content with max retries
25.Take a base url and a {@link Map} of parameters to build a valid url