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:com.dougtest.restTest.getJsonInfo.java

public static JsonNode retrieveResourceFromResponse(final HttpResponse response) throws IOException {
    final String jsonFromResponse = EntityUtils.toString(response.getEntity());
    final ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(jsonFromResponse);
    return root;//from   ww  w.java 2s. co m
}

From source file:Main.java

public static String getJSONFromUrl(String url) {

    try {/*from  www  .  j  a  v a2s .  co  m*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        //            httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static String getJSONFromUrlWithPostRequest(String url, List<NameValuePair> params) {

    try {//  w w w . j  ava 2  s. co  m
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.lushell.tc.dbpaas.api.utils.MyHttpUtil.java

public static String getDataFromUrl(String url, int timeout) {
    try {/* w  w  w. j  a  va2s.  c o  m*/
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        //StatusLine statusLine = response.getStatusLine();
        String body = EntityUtils.toString(entity);
        return body;
    } catch (IOException ex) {
        Logger.getLogger(MyHttpUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:Main.java

/**
 * Code adapted from http://stackoverflow.com/a/12854981
 * @return Device external IP address or ERROR statement.
 *//*from w  ww.ja v a2  s  . co m*/
public static String getExternalIP() {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
        // HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
        // HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null && entity.getContentLength() > 0) {
            JSONObject json_data = new JSONObject(EntityUtils.toString(entity));
            return json_data.getString("ip");
        } else {
            Log.e(LOG_TAG, "Response error: " + response.getStatusLine().toString());
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, e.toString());
    }
    return "ERROR";
}

From source file:org.apache.unomi.itests.TestUtils.java

public static <T> T retrieveResourceFromResponse(HttpResponse response, Class<T> clazz) throws IOException {
    String jsonFromResponse = EntityUtils.toString(response.getEntity());
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
            false);//from w  w w.  j  a v  a 2s  .  com
    return mapper.readValue(jsonFromResponse, clazz);
}

From source file:com.phonemetra.turbo.lockclock.weather.HttpRetriever.java

public static String retrieve(String url) {
    HttpGet request = new HttpGet(url);
    try {/*from   w  w  w.ja  va 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) {

    }
    return null;
}

From source file:Main.java

public static String getRepsonseString(String url, Map<String, String> params) {
    HttpPost request = new HttpPost(url);
    List<NameValuePair> p = new ArrayList<NameValuePair>();
    for (String key : params.keySet()) {
        p.add(new BasicNameValuePair(key, params.get(key)));
    }//ww w  .  j a v a 2  s. c om
    HttpClient httpClient = new DefaultHttpClient();
    String result = null;

    try {
        request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            result = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO_8859_1"), "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (httpClient != null)
            httpClient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:org.zalando.stups.tokens.AccessTokenEndpointException.java

public static AccessTokenEndpointException from(HttpResponse response) {
    final StringBuilder message = new StringBuilder();
    message.append(response.getStatusLine().toString());

    final HttpEntity entity = response.getEntity();
    if (entity != null) {
        message.append("\n").append("Response Body:").append("\n");
        try {/*from www.j a v a  2 s. com*/
            message.append(EntityUtils.toString(entity));
        } catch (IOException e) {
            message.append("<<Error Reading Response Body>>");
        }
    }

    return new AccessTokenEndpointException(message.toString());
}

From source file:com.cyanogenmod.lockclock.weather.HttpRetriever.java

public static String retrieve(String url) {
    HttpGet request = new HttpGet(url);
    try {//from   w w  w  .  ja v  a 2 s. c om
        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", e);
    }
    return null;
}