Example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

Introduction

In this page you can find the example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity.

Prototype

public UrlEncodedFormEntity(final Iterable<? extends NameValuePair> parameters, final Charset charset) 

Source Link

Document

Constructs a new UrlEncodedFormEntity with the list of parameters in the specified encoding.

Usage

From source file:Main.java

public static String postReqAsJson(String uri, String requestJson) throws ClientProtocolException, IOException {
    Log.i(TAG, "Send data to :" + uri + " ========== and the data str:" + requestJson);
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("attendanceClientJSON", requestJson));
    post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String retStr = EntityUtils.toString(response.getEntity());
        Log.i(TAG, "=================response str:" + retStr);
        return retStr;
    }/*from   ww w .  j a  v  a2  s  .c om*/

    return response.getStatusLine().getStatusCode() + "ERROR";
}

From source file:Main.java

public static String getResultPost(String uri, List<NameValuePair> params) {
    String Result = null;//from  w ww  .j a  va2s.  c o  m
    try {
        if (uri == null) {
            return "";
        }
        HttpPost httpRequest = new HttpPost(uri);
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpRequest);
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {

            StringBuilder builder = new StringBuilder();
            BufferedReader bufferedReader2 = new BufferedReader(
                    new InputStreamReader(httpResponse.getEntity().getContent()));
            for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) {
                builder.append(s);
            }
            Result = builder.toString();

        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Result;
}

From source file:Main.java

public static String postReqAsJsonAddParam(String uri, String requestJson, Map<String, String> param)
        throws ClientProtocolException, IOException {
    Log.i(TAG, "Send data to :" + uri + " ========== and the data str:" + requestJson);
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : param.entrySet()) {
        parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }//from  ww  w.ja v a2 s.com
    parameters.add(new BasicNameValuePair("attendanceClientJSON", requestJson));
    post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String retStr = EntityUtils.toString(response.getEntity());
        Log.i(TAG, "=================response str:" + retStr);
        return retStr;
    }

    return response.getStatusLine().getStatusCode() + "ERROR";
}

From source file:uk.ac.ebi.atlas.utils.HttpRequest.java

public static InputStream httpPost(org.apache.http.client.HttpClient httpClient, String url,
        List<? extends NameValuePair> params) throws IOException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
        return response.getEntity().getContent();
    }//from   ww w .j  a  v a 2  s. c om
    throw new IOException(
            "Server returned invalid response: [status_code = " + statusCode + "; url = " + url + "]");
}

From source file:com.zhaosen.util.HttpClientUtil.java

License:asdf

@SuppressWarnings({ "rawtypes", "unchecked" })
public static String httpPost(String url, String param) throws ClientProtocolException, IOException {
    new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    ArrayList params = new ArrayList();
    params.add(new BasicNameValuePair("data", param));
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = (new DefaultHttpClient()).execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        String result = EntityUtils.toString(response.getEntity());
        return result;
    } else {//from w ww .j  ava 2 s  .co m
        return "";
    }
}

From source file:costumetrade.common.wenqian.util.WQSignUtils.java

/**
 * ???//from   w  w w . ja  v  a 2  s  .  com
 * 
 * @return
 */
public static String getToken() {

    List<BasicNameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("client_id", WQAPIConstants.CLIENT_ID));
    nvps.add(new BasicNameValuePair("client_secret", WQAPIConstants.CLIENT_SECRET));
    nvps.add(new BasicNameValuePair("grant_type", WQAPIConstants.GRANT_TYPE));
    nvps.add(new BasicNameValuePair("scope", WQAPIConstants.SCOPE));

    String response = HttpClientUtils.doPost(WQAPIConstants.token_api,
            new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
    JSONObject responseJSON = JSONObject.parseObject(response);
    if (responseJSON.containsKey("access_token")) {
        return responseJSON.get("access_token").toString();
    }
    throw new RuntimeException("???");
}

From source file:org.guohai.android.cta.utility.HttpRest.java

/**
 * HTTPPost?//from  ww  w  . ja v a  2 s.  c om
 * @param url POST?
 * @param pairs ?
 * @return ?JOSN?null
 */
public static ResultInfo HttpPostClient(String url, List<NameValuePair> pairs) {
    //create http client
    HttpClient client = new DefaultHttpClient();
    //create post request
    HttpPost httpPost = new HttpPost(url);
    //return value
    ResultInfo result = new ResultInfo();
    //init eroor value
    result.State = -1000;
    result.Message = "posterror";

    HttpEntity entity;
    try {
        entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8);

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
        return result;
    }
    httpPost.setEntity(entity);

    //??POST? 
    try {
        HttpResponse response = client.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entityHtml = response.getEntity();
            BufferedReader reader = new BufferedReader(new InputStreamReader(entityHtml.getContent(), "UTF-8"));
            String line = null;
            String reString = "";
            while ((line = reader.readLine()) != null) {
                reString += line;
            }
            if (entityHtml != null) {
                entityHtml.consumeContent();
            }
            Log.i(TAG, reString);
            JSONObject jsonObj;

            try {
                jsonObj = new JSONObject(reString);
                result.State = jsonObj.getInt("state");
                result.Message = jsonObj.getString("message");
                return result;
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
            }
            return result;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG, e.getMessage());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }
    return result;
}

From source file:com.fengduo.bee.commons.util.HttpClientUtils.java

public static String postRequest(String url, List<NameValuePair> postParams) throws Exception {
    HttpPost post = new HttpPost(url);
    UrlEncodedFormEntity uefEntity;//from  ww  w . j  a  v a  2s  .c om
    String result = null;
    try {
        uefEntity = new UrlEncodedFormEntity(postParams, CHARSET);
        post.setEntity(uefEntity);
        HttpResponse rep = client.execute(post);
        if (rep.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = rep.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
            }
        }

    } catch (Exception e) {
        throw e;
    } finally {
        post.abort();
    }
    return result;
}

From source file:Main.java

public static byte[] doPostSubmit(String url, Map<String, Object> params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost();
    try {/*from  w ww . j a  v  a 2s  .co m*/

        List<NameValuePair> list = new ArrayList<NameValuePair>();

        for (Entry<String, Object> entry : params.entrySet()) {
            NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
            list.add(nameValuePair);
        }
        httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toByteArray(entity);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.mumod.util.MustardUtil.java

public static void snapshot(Context context, String id, String version, String accountNumber) {
    try {// www. jav  a 2  s. c  om
        HttpPost post = new HttpPost("http://mustard.macno.org/snapshot.php");
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("v", version));
        params.add(new BasicNameValuePair("n", accountNumber));
        params.add(new BasicNameValuePair("m", id));
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpManager hm = new HttpManager(context);
        DefaultHttpClient hc = hm.getHttpClient();
        hc.execute(post);
    } catch (Exception e) {
    }
}