List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:cn.org.once.cstack.cli.exception.CustomResponseErrorHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else {//w ww.jav a 2 s.c o m switch (status) { case 500: InputStreamReader reader = null; reader = new InputStreamReader(response.getEntity().getContent()); LineIterator lineIterator = new LineIterator(reader); StringBuilder jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } HttpErrorServer error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); case 401: throw new ClientProtocolException("Status 401 - Bad credentials!"); case 403: throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!"); case 404: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); default: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); } } }
From source file:azkaban.executor.ExecutorApiClient.java
/**Implementing the parseResponse function to return de-serialized Json object. * @param response the returned response from the HttpClient. * @return de-serialized object from Json or null if the response doesn't have a body. * *//*from ww w . jav a 2s . co m*/ @Override protected String parseResponse(HttpResponse response) throws HttpResponseException, IOException { final StatusLine statusLine = response.getStatusLine(); String responseBody = response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : ""; if (statusLine.getStatusCode() >= 300) { logger.error(String.format("unable to parse response as the response status is %s", statusLine.getStatusCode())); throw new HttpResponseException(statusLine.getStatusCode(), responseBody); } return responseBody; }
From source file:com.github.ignition.support.http.IgnitedHttpResponseImpl.java
@Override public String getResponseBodyAsString() throws IOException { if (entity == null) { return null; }//from w w w. j a v a2 s . c o m return EntityUtils.toString(entity); }
From source file:com.aremaitch.codestock2010.datadownloader.ScheduleBuilder.java
public long getUserIDFromEmail(String emailAddress) { long userid = 0; DefaultHttpClient hc = new DefaultHttpClient(); HttpGet hg = new HttpGet(userIDFromEmailURL + "?" + userIDFromEmailSvc + "=" + emailAddress); HttpResponse response;/* w w w . j ava2 s . c o m*/ try { response = hc.execute(hg); String queryResult = EntityUtils.toString(response.getEntity()); JSONObject jObj = new JSONObject(queryResult); userid = jObj.getLong("d"); } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return userid; }
From source file:fr.treeptik.cloudunit.cli.exception.CustomResponseErrorHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else {/* w w w . j a v a 2s . co m*/ switch (status) { case 500: InputStreamReader reader = null; reader = new InputStreamReader(response.getEntity().getContent()); LineIterator lineIterator = new LineIterator(reader); StringBuilder jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } JsonResponseError error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); case 401: throw new ClientProtocolException("Status 401 - Bad credentials!"); case 403: throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!"); case 404: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); default: reader = null; reader = new InputStreamReader(response.getEntity().getContent()); lineIterator = new LineIterator(reader); jsonStringBuilder = new StringBuilder(); while (lineIterator.hasNext()) { jsonStringBuilder.append(lineIterator.nextLine()); } error = JsonConverter.getError(jsonStringBuilder.toString()); throw new ClientProtocolException(error.getMessage()); } } }
From source file:com.rackspacecloud.blueflood.outputs.handlers.HttpEventsQueryHandlerIntegrationTest.java
@Test public void testHttpEventsQueryHandler_HappyCase() throws Exception { parameterMap = new HashMap<String, String>(); parameterMap.put(Event.fromParameterName, String.valueOf(baseMillis - 86400000)); parameterMap.put(Event.untilParameterName, String.valueOf(baseMillis + (86400000 * 3))); HttpGet get = new HttpGet(getQueryEventsURI(tenantId)); HttpResponse response = client.execute(get); String responseString = EntityUtils.toString(response.getEntity()); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertFalse(responseString.equals("[]")); assertResponseHeaderAllowOrigin(response); }
From source file:neembuu.release1.httpclient.utils.NHttpClientUtils.java
/** * Get the content of a page.// www.j a v a2 s . c om * @param url url from which to read * @return the String content of the page * @throws Exception */ public static String getData(String url, DefaultHttpClient httpClient) throws Exception { HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity()); }
From source file:org.opencastproject.remotetest.util.JobUtils.java
/** * Gets the xml representation of a job. * /* w w w . j a va2s. c om*/ * @param jobId * the job identifier * @return the job as xml * @throws IOException * if the job could not be loaded */ public static String getJobAsXml(String jobId) throws IOException { HttpGet getWorkflowMethod = new HttpGet(BASE_URL + "/services/job/" + jobId + ".xml"); TrustedHttpClient client = Main.getClient(); try { HttpResponse response = client.execute(getWorkflowMethod); if (response.getStatusLine().getStatusCode() != 200) throw new IllegalStateException(EntityUtils.toString(response.getEntity())); String job = EntityUtils.toString(response.getEntity()); return job; } finally { Main.returnClient(client); } }
From source file:ac.robinson.paperchains.SoundCloudUrlFetcherTask.java
@Override protected String doInBackground(Long... trackIds) { if (trackIds.length < 1) { return null; }//from w ww . ja va 2 s. c o m final long trackId = trackIds[0]; try { HttpResponse trackResponse = mWrapper.get(Request.to(Endpoints.TRACK_DETAILS, trackId)); if (trackResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { JSONObject trackJSON = new JSONObject(EntityUtils.toString(trackResponse.getEntity())); if (trackJSON.getBoolean("streamable")) { // should always be the case HttpResponse streamResponse = mWrapper.get(Request.to("/tracks/%d/stream", trackId)); JSONObject streamJSON = new JSONObject(EntityUtils.toString(streamResponse.getEntity())); return streamJSON.getString("location"); } errorReason = R.string.hint_soundcloud_load_too_early; return null; } else { errorReason = R.string.hint_soundcloud_load_too_early; return null; } } catch (IOException e) { return null; } catch (ParseException e) { return null; } catch (JSONException e) { return null; } }
From source file:co.edu.uniajc.vtf.utils.RestAsyncTask.java
@Override protected String doInBackground(String... params) { String lsResult = ""; try {/*from w ww . j a v a 2s . c o m*/ if (params[0].equals("0")) { HttpGet loRequest = new HttpGet(params[1].toString()); HttpResponse loResponse = coClient.execute(loRequest); lsResult = EntityUtils.toString(loResponse.getEntity()); } else if (params[0].equals("1")) { HttpPost loRequest = new HttpPost(params[1].toString()); StringEntity lsData = new StringEntity(params[2].toString()); loRequest.setEntity(lsData); loRequest.setHeader("Accept", "application/json"); loRequest.setHeader("Content-type", "application/json"); HttpResponse loResponse = coClient.execute(loRequest); lsResult = EntityUtils.toString(loResponse.getEntity()); } } catch (SocketTimeoutException ex) { this.cbohasError = true; lsResult = "Timeout exception"; } catch (Exception ex) { this.cbohasError = true; lsResult = ex.getMessage(); } return lsResult; }