List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity, String str) throws IOException, ParseException
From source file:nl.ivonet.io.WebResource.java
public String getJson(final String url) { final HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Accept", "application/json"); try {/*www.ja v a 2s . c om*/ final HttpResponse response = client.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (final IOException e) { throw new RuntimeException(e); } return ""; }
From source file:eu.over9000.skadi.util.HttpUtil.java
public static String getAPIResponse(final String apiUrl) throws URISyntaxException, IOException { final URI URL = new URI(apiUrl); final HttpGet request = new HttpGet(URL); request.setHeader("Client-ID", CLIENT_ID); final HttpResponse response = HTTP_CLIENT.execute(request); return EntityUtils.toString(response.getEntity(), "UTF-8"); }
From source file:org.dspace.identifier.ezid.EZIDResponse.java
public EZIDResponse(HttpResponse response) throws IdentifierException { this.response = response; HttpEntity responseBody = response.getEntity(); // Collect the content of the percent-encoded response. String body;/* www. jav a2s . c o m*/ try { body = EntityUtils.toString(responseBody, UTF_8); } catch (IOException ex) { log.error(ex.getMessage()); throw new IdentifierException("EZID response not understood: " + ex.getMessage()); } catch (ParseException ex) { log.error(ex.getMessage()); throw new IdentifierException("EZID response not understood: " + ex.getMessage()); } String[] parts; String[] lines = body.split("[\\n\\r]"); // First line is request status and message or value parts = lines[0].split(":", 2); status = parts[0].trim(); if (parts.length > 1) { statusValue = parts[1].trim(); } else { statusValue = null; } // Remaining lines are key: value pairs for (int i = 1; i < lines.length; i++) { parts = lines[i].split(":", 2); String key = null, value = null; try { key = URLDecoder.decode(parts[0], UTF_8).trim(); if (parts.length > 1) { value = URLDecoder.decode(parts[1], UTF_8).trim(); } else { value = null; } } catch (UnsupportedEncodingException e) { // XXX SNH, we always use UTF-8 which is required by the Java spec. } attributes.put(key, value); } }
From source file:io.alicorn.device.client.DeviceClient.java
private static String apacheHttpEntityToString(HttpEntity entity) { try {//from w w w .jav a2 s . c o m if (entity != null) { String encoding = "UTF-8"; if (entity.getContentEncoding() != null) { encoding = entity.getContentEncoding().getValue(); encoding = encoding == null ? "UTF-8" : encoding; } return EntityUtils.toString(entity, encoding); } else { logger.error("Cannot parse a null ApacheHttpEntity."); } } catch (IOException e) { logger.error("Unexpected IO error when parsing entity content [" + e.getMessage() + "].", e); } return ""; }
From source file:uk.bcu.services.ItunesSearchService.java
/** This is making api calls */ public void run() { //String api_key = "096174e14f0efd0be330fce5f1f84de2"; String url = //"https://itunes.apple.com/search?term="+query1+"&entity=album"; "http://itunes.apple.com/search?term=" + query1 + "&media=movie"; //"http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=" + // api_key+ "&q="+query; boolean error = false; HttpClient httpclient = null;// w w w .j av a2 s.co 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 (json.getInt("resultCount") > 0) { resultss = json.getJSONArray("results"); } else { error = true; } } catch (Exception e) { resultss = null; error = true; } finally { httpclient.getConnectionManager().shutdown(); } super.serviceComplete(error); }
From source file:com.spider.java
public static String downloadPage(String path) throws Exception { // ? //from w w w . j a v a 2 s . c om HttpGet httpget = new HttpGet(path); String result = ""; try { // get ? HttpResponse response = httpCLient.execute(httpget); // ??? HttpEntity entity = response.getEntity(); if (entity != null) { // result=EntityUtils.toString(entity,"utf-8"); if (entity != null) { result = EntityUtils.toString(entity, "utf-8"); } // System.out.println(result); httpget.abort(); } } catch (ClientProtocolException e) { } catch (IOException e) { } return result; }
From source file:com.simple.toadiot.rtinfosdk.http.DefaultResponseHandler.java
public Response handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int statusCode = response.getStatusLine().getStatusCode(); if (!isHttpStatusOK(statusCode)) { String errorDetail = null; try {/*from w ww. j av a 2s . c o m*/ errorDetail = EntityUtils.toString(response.getEntity(), AppConfig.getCharset()); } catch (IOException swallow) { } throw new HttpException("Http response " + statusCode, statusCode, errorDetail); } Response retval = new Response(statusCode); try { retval.setBody(parseHttpEntity(response.getEntity())); } catch (IOException e) { throw new HttpException("Http response [%s] but body cannot be parsed.", e); } catch (ParseException e) { // TODO Auto-generated catch block throw new ParseToObjectException("Json?", e); } catch (JSONException e) { // TODO Auto-generated catch block throw new ParseToObjectException("Json?", e); } Map<String, String> headers = new HashMap<String, String>(); for (Header header : response.getAllHeaders()) { headers.put(header.getName(), header.getValue()); } retval.setHeaders(headers); return retval; }
From source file:com.wudaosoft.net.httpclient.StringResponseHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); Charset charset = ContentType.getOrDefault(entity).getCharset(); if (charset == null) { charset = Consts.UTF_8;/*w w w . java 2s. co m*/ } return entity != null ? EntityUtils.toString(entity, charset) : ""; } else { throw new ClientProtocolException("Unexpected response status: " + status); } }
From source file:com.intellij.tasks.impl.httpclient.ResponseUtil.java
public static String getResponseContentAsString(@Nonnull HttpResponse response) throws IOException { return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET); }
From source file:HelloWorld.HelloWorldTest.java
@Test public void getPeopleNotExist() throws URISyntaxException, IOException { URI uri = new URI(baseURI + "api/person/sally"); HttpGet get = new HttpGet(uri); CloseableHttpResponse response = client.execute(get); assertEquals(404, response.getStatusLine().getStatusCode()); assertEquals("No person exists with specified name.", EntityUtils.toString(response.getEntity(), "UTF-8")); }