Example usage for org.apache.http.entity StringEntity setContentEncoding

List of usage examples for org.apache.http.entity StringEntity setContentEncoding

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity setContentEncoding.

Prototype

public void setContentEncoding(Header header) 

Source Link

Usage

From source file:net.tsz.afinal.FinalHttp.java

public void post(String url, JSONObject params, AjaxCallBack<? extends Object> callBack)
        throws UnsupportedEncodingException {
    StringEntity entity = new StringEntity(params.toString(), "utf-8");// ?
    entity.setContentEncoding("UTF-8");
    entity.setContentType("application/json");
    post(url, entity, null, callBack);/*from w  ww.j a v a 2 s.  co m*/
}

From source file:org.openo.portal.system.RegisterService.java

private static String registerPortalService(String serviceName, String url, JSONObject json, String token) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    String response = null;/*from   w  w w.  ja  v  a 2  s  . c o m*/

    try {
        if (null != json) {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
        }
        if (!CommonUtil.isEmpty(token)) {
            post.addHeader("X-Auth-Token", token);
        }
        HttpResponse res = client.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK
                || res.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            String result = EntityUtils.toString(res.getEntity());
            if (!CommonUtil.isEmpty(result)) {
                response = result;
            } else {
                response = null;
            }
        }
        logger.info("register task [" + serviceName + "] completed successfully.");
    } catch (Exception e) {
        logger.error("register task [" + serviceName + "] failed because of errors.");
        logger.error(e.getMessage());
    }

    return response;
}