Example usage for org.apache.http.util EntityUtils toString

List of usage examples for org.apache.http.util EntityUtils toString

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toString.

Prototype

public static String toString(HttpEntity httpEntity, String str) throws IOException, ParseException 

Source Link

Usage

From source file:com.jzboy.couchdb.http.CouchResponseHandler.java

/**
 * Returns the response body as a String if the response was successful (a
 * 2xx status code). If no response body exists, this returns null. If the
 * response was unsuccessful (>= 300 status code), throws an
 * {@link HttpResponseException}.//ww w  .  j a va 2  s  .c om
 */
@Override
public CouchResponse handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
    StatusLine statusLine = response.getStatusLine();
    String body = null;
    HttpEntity entity = response.getEntity();
    if (entity != null)
        body = EntityUtils.toString(entity, "utf-8");

    return new CouchResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase(), body);
}

From source file:uk.bcu.services.MusicSearchService.java

public void run() {
    String api_key = "f06e41f0aad377a1aebfe76927318181";
    String url = "http://ws.audioscrobbler.com/2.0/?method=album.search&album=" + query + "&api_key=" + api_key
            + "&format=json";

    boolean error = false;
    HttpClient httpclient = null;/* w  w  w . jav  a 2  s  .  c o m*/
    try {
        httpclient = new DefaultHttpClient();
        HttpResponse data = httpclient.execute(new HttpGet(url));
        HttpEntity entity = data.getEntity();
        String result = EntityUtils.toString(entity, "UTF8");

        JSONObject json = new JSONObject(result);
        if (Integer.valueOf(json.getJSONObject("results").getString("opensearch:totalResults")) > 0) {
            results = json.getJSONObject("results").getJSONObject("albummatches").getJSONArray("album");
        } else {
            error = true;
        }
    } catch (Exception e) {
        results = null;
        error = true;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    super.serviceComplete(error);
}

From source file:com.cognifide.aet.common.JsonResponseHandler.java

@Override
public T handleResponse(HttpResponse httpResponse) throws IOException {
    StatusLine statusLine = httpResponse.getStatusLine();
    if (statusLine.getStatusCode() == 200) {
        String result = EntityUtils.toString(httpResponse.getEntity(), MIME.UTF8_CHARSET);
        Gson gson = new Gson();
        return gson.fromJson(result, resultClass);
    } else {//from ww  w . java2s  . co  m
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
}

From source file:webrequester.CouchDBWebRequest.java

public String requestWithPut(String content) {
    String s = "";
    try {/*  w  w w  .  j  a va2 s .  c  om*/

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut httpput = new HttpPut(buildURI().toString());

        StringEntity se = new StringEntity(content);
        httpput.setEntity(se);

        CloseableHttpResponse response = httpclient.execute(httpput);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                s = EntityUtils.toString(entity, "UTF-8");
            }
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        return null;
    }
    return s;
}

From source file:tv.icntv.common.HttpClientUtil.java

public static String getContent(String url, Charset charset) throws IOException {
    HttpGet request = new HttpGet(url);
    request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build());
    CloseableHttpClient client = HttpClientHolder.getClient();
    CloseableHttpResponse response = null;

    response = client.execute(request);//from  w ww. j a  v a  2s  .  com
    return EntityUtils.toString(response.getEntity(), charset);

}

From source file:uk.bcu.services.MusicDetailsService.java

public void run() {
    String api_key = "f06e41f0aad377a1aebfe76927318181";
    String url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=" + api_key + "&mbid="
            + movieId + "&format=json";

    Log.i("URL", url);

    boolean error = false;
    HttpClient httpclient = null;/*  ww w  .ja v a2s.c  o m*/
    try {
        httpclient = new DefaultHttpClient();
        HttpResponse data = httpclient.execute(new HttpGet(url));
        HttpEntity entity = data.getEntity();
        String result = EntityUtils.toString(entity, "UTF8");

        JSONresult = new JSONObject(result);

        //if (Integer.valueOf(json.getJSONObject("track").getString("opensearch)) > 0) {
        //  results = json.getJSONObject("results").getJSONObject("trackmatches").getJSONArray("track");
    } catch (Exception e) {
        System.out.println(e.toString());
        movie = null;
        error = true;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    super.serviceComplete(error);
}

From source file:com.ssy.havefunweb.util.WeixinUtil.java

public static JSONObject doPostStr(String url, String outStr) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);
    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
    JSONObject jsonObject = JSONObject.fromObject(result);
    return jsonObject;
}

From source file:communication.Communicator.java

/**
 * Adds a new trackingPosition to an existing cartracker
 *
 * @param tracker The cartracker with a new trackingPosition
 * @return The serialnumber of the new trackingPosition
 * @throws IOException//from   w w  w.j a v a 2 s . co m
 */
public static Long postTrackingPositionsForTracker(CarTracker tracker) throws IOException {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(BASE_URL_PRODUCTION + "/" + tracker.getId() + "/movements");
    String jsonBody = gson.toJson(tracker.getCurrentTrackingPeriod());
    StringEntity postingString = new StringEntity(jsonBody, CHARACTER_SET);
    post.setEntity(postingString);
    post.setHeader(HTTP.CONTENT_TYPE, "application/json");
    HttpResponse response = httpClient.execute(post);

    String responseString = EntityUtils.toString(response.getEntity(), CHARACTER_SET);
    JSONObject json = new JSONObject(responseString);
    return json.getLong("serialNumber");
}

From source file:kcb.billerengine.processors.CallJSON.java

private String getJson(HttpClient httpClient, String jsonURL, String body) {
    String sb = null;/*w ww.  ja va  2s  .  co m*/
    try {
        System.out.println("JSON REQUEST: " + body);
        StringEntity params = new StringEntity(body);

        HttpPost request = new HttpPost(jsonURL);
        request.addHeader("content-type", "application/text");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        sb = EntityUtils.toString(response.getEntity(), "UTF-8");

    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    System.out.println("ECITIZEN RESPONSE : " + sb);
    httpClient.getConnectionManager().shutdown();
    return sb;
}

From source file:me.zhuoran.crawler4j.crawler.http.HttpFetchResult.java

public HttpFetchResult(HttpResponse rep, String defaultCharset) throws Exception {
    entity = rep.getEntity();/*from ww w.j  a  v  a  2s .com*/
    status = rep.getStatusLine();
    this.charset = defaultCharset;
    if (entity.getContentType() != null && entity.getContentType().getValue().contains("text/html")) {
        html = EntityUtils.toString(entity, charset);
    } else {
        contentData = EntityUtils.toByteArray(entity);
    }
}