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) throws IOException, ParseException 

Source Link

Usage

From source file:org.apache.activemq.artemis.tests.integration.rest.util.ResponseUtil.java

public static String getDetails(CloseableHttpResponse response) throws IOException {
    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity);
}

From source file:pl.raszkowski.sporttrackersconnector.helper.HttpResponseConverter.java

public static String getAsString(HttpResponse response) {
    HttpEntity entity = response.getEntity();

    if (entity == null) {
        LOG.debug("Entity is null, response = {}, status code = {}.", response,
                response.getStatusLine().getStatusCode());
        return "";
    }/*from   w w  w . j a  v a  2s  .c om*/

    try {
        return EntityUtils.toString(entity);
    } catch (IOException e) {
        LOG.error("Unable to convert entity to string.", e);
        return "";
    }
}

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;
    }//  w ww.  ja va2  s .  c  om

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

From source file:com.cdhxqh.travel_ticket_app.weather.HttpRetriever.java

public static String retrieve(String url) {
    HttpGet request = new HttpGet(url);
    try {// w ww  . j a  v  a 2 s .  c  o m
        HttpResponse response = new DefaultHttpClient().execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toString(entity);
        }
    } catch (IOException e) {
        Log.e(TAG, "Couldn't retrieve data from url " + url, e);
    }
    return null;
}

From source file:pingdesktop.Model.HttpHandler.java

public static String doGet(String page_url) throws IOException {

    /* create the HTTP client and GET request */
    HttpGet httpGet = new HttpGet(BaseUrl + page_url);

    /* execute request */
    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = httpResponse.getEntity();

    /* process response */
    if (httpResponse.getStatusLine().getStatusCode() == 200) {
        String responseText = EntityUtils.toString(httpEntity);
        return responseText;
    } else {/*from w  w  w  .j  a va  2s . c  o m*/
        return "Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode();
    }

}

From source file:com.sender.request.SenderUtil.java

public static void post() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/ServicioWebUtil/ServicioUtil?wsdl");
    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:util=\"http://util.servicio.com/\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <util:saludar/>\n"
            + "   </soapenv:Body>\n" + "</soapenv:Envelope>";
    HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
    post.setEntity(entity);/*from ww w. ja v  a 2 s.  co  m*/
    HttpResponse response = client.execute(post);
    String result = EntityUtils.toString(response.getEntity());

    System.out.println("Resultado: " + result);
}

From source file:net.locosoft.fold.neo4j.internal.Neo4jRestUtil.java

public static JsonObject doGetJson(String uri) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        CloseableHttpResponse response = httpClient.execute(new HttpGet(uri));

        String bodyText = EntityUtils.toString(response.getEntity());
        JsonObject jsonObject = JsonObject.readFrom(bodyText);
        return jsonObject;
    } catch (Exception ex) {
        ex.printStackTrace();//from   w  w  w .  j  av  a  2s . c  om
    }
    return null;
}

From source file:com.earth.places.utilities.JSONParser.java

public static JSONObject getJSONFromURL(String url) {

    try {/*from   w  w  w.j  a va 2  s . co  m*/

        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(new HttpGet(url));

        if (response.getStatusLine().getStatusCode() == 200) {

            final String data = EntityUtils.toString(response.getEntity());
            return new JSONObject(data);

        }

    } catch (Exception e) {
        Log.d(TAG, "No response from the server, where you have stored your json: " + e.getMessage());
    }

    return null;
}

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   www. j  av  a  2 s  .c  o m
    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:Main.java

public static boolean getkeepaliveinfo() {
    String baseUrl = "http://10.6.8.2/cgi-bin/keeplive?";

    try {/* w  w  w .j  a  v  a2  s  . co m*/
        HttpGet getMethod = new HttpGet(baseUrl);
        getMethod.addHeader("Accept", "*/*");
        //getMethod.addHeader("Accept-Language", "zh-cn");
        //getMethod.addHeader("Referer", "http://202.117.2.41/index.html");
        //getMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");
        //getMethod.addHeader("Accept-Encoding", "gzip, deflate");
        //getMethod.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
        getMethod.addHeader("Host", "10.6.8.2");
        //getMethod.addHeader("DNT", "1");

        HttpClient httpClient = new DefaultHttpClient();

        HttpResponse response = httpClient.execute(getMethod);
        Log.i(TAG, "Sending message.....");
        HttpEntity httpEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            String message = EntityUtils.toString(httpEntity);
            if (httpEntity == null || message.compareTo("error") == 0) {
                Log.i(TAG, "Get keepalive info failed!!!message=" + message);
                return false;
            } else {
                Log.i(TAG, "Get keepalive info succeed!!!message=" + message);
                return true;
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.i(TAG, "Get keepalive info failed!!!");
    return false;
}