List of usage examples for org.apache.http.client HttpResponseException HttpResponseException
public HttpResponseException(final int statusCode, final String s)
From source file:com.flipzu.flipzu.FlipInterface.java
private JSONArray sendJson(String url, String data) throws IOException { DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }/*ww w . j a v a 2 s. c om*/ HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); } }; HttpPost postMethod = new HttpPost(url); postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); if (data != null) { StringEntity tmp = null; try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { debug.logE(TAG, "sendJson ERROR", e.getCause()); return null; } postMethod.setEntity(tmp); } String response = hc.execute(postMethod, res); JSONTokener tokener = new JSONTokener(response); JSONArray jobj = null; try { jobj = new JSONArray(tokener); } catch (JSONException e) { debug.logE(TAG, "sendJson got exception " + response, e.getCause()); return null; } debug.logV(TAG, "sendJson got " + jobj); return jobj; }
From source file:com.flipzu.flipzu.FlipInterface.java
private FlipUser setFollowUnfollow(String username, String token, boolean follow) throws IOException { String data = "username=" + username + "&access_token=" + token; String url;/* w w w . j a va 2 s . com*/ if (follow) { url = WSServer + "/api/set_follow.xml"; } else { url = WSServer + "/api/set_unfollow.xml"; } debug.logV(TAG, "setFollow for username " + username); DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); } }; HttpPost postMethod = new HttpPost(url); postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); if (data != null) { StringEntity tmp = null; try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { debug.logE(TAG, "getUser ERROR", e.getCause()); return null; } postMethod.setEntity(tmp); } String response = hc.execute(postMethod, res); SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); UserHandler myUserHandler = new UserHandler(); xr.setContentHandler(myUserHandler); InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource); FlipUser parsedData = myUserHandler.getParsedData(); return parsedData; } catch (ParserConfigurationException e) { return null; } catch (SAXException e) { return null; } }
From source file:net.tsz.afinal.http.HttpHandler.java
private void handleResponse(HttpResponse response) { StatusLine status = response.getStatusLine(); ZLogger.d(String.format("handleResponse:%d/%s", status.getStatusCode(), status.getReasonPhrase())); if (status.getStatusCode() >= 300) { String errorMsg = "response status error code: " + status.getStatusCode(); if (status.getStatusCode() == 416 && isResume) { errorMsg += " \n maybe you have download complete."; }//from ww w. j a va 2 s .c o m publishProgress(UPDATE_FAILURE, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), errorMsg); } else { try { HttpEntity entity = response.getEntity(); Object responseBody = null; if (entity != null) { time = SystemClock.uptimeMillis(); if (targetUrl != null) { responseBody = mFileEntityHandler.handleEntity(entity, this, targetUrl, isResume); } else { responseBody = mStrEntityHandler.handleEntity(entity, this, charset); } } publishProgress(UPDATE_SUCCESS, responseBody); } catch (IOException e) { publishProgress(UPDATE_FAILURE, e, e.getMessage()); } } }
From source file:synapticloop.b2.request.BaseB2Request.java
/** * Execute an HTTP HEAD request and return the response for further parsing * * @return the response object// w w w . ja v a2 s . c o m * * @throws B2ApiException if something went wrong with the call * @throws IOException if there was an error communicating with the API service */ protected CloseableHttpResponse executeHead() throws B2ApiException, IOException { URI uri = this.buildUri(); LOGGER.debug("HEAD request to URL '{}'", uri.toString()); HttpHead httpHead = new HttpHead(uri); CloseableHttpResponse httpResponse = this.execute(httpHead); switch (httpResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return httpResponse; } final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()), new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase())); if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) { throw failure .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue())); } throw failure; }
From source file:synapticloop.b2.request.BaseB2Request.java
/** * Execute a GET request, returning the data stream from the response. * * @return The response from the GET request * * @throws B2ApiException if there was an error with the request * @throws IOException if there was an error communicating with the API service *///from w ww . ja v a2s. c o m protected CloseableHttpResponse executeGet() throws B2ApiException, IOException { URI uri = this.buildUri(); HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse httpResponse = this.execute(httpGet); // you will either get an OK or a partial content switch (httpResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return httpResponse; } final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()), new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase())); if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) { throw failure .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue())); } throw failure; }
From source file:synapticloop.b2.request.BaseB2Request.java
/** * Execute a POST request returning the response data as a String * * @return the response data as a string * * @throws B2ApiException if there was an error with the call, most notably * a non OK status code (i.e. not 200) * @throws IOException if there was an error communicating with the API service *//*from w w w .j a v a2s. c o m*/ protected CloseableHttpResponse executePost() throws B2ApiException, IOException { URI uri = this.buildUri(); String postData = convertPostData(); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new StringEntity(postData, APPLICATION_JSON)); CloseableHttpResponse httpResponse = this.execute(httpPost); switch (httpResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return httpResponse; } final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()), new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase())); if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) { throw failure .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue())); } throw failure; }
From source file:synapticloop.b2.request.BaseB2Request.java
/** * Execute a POST request with the contents of a file. * * @param entity Content to write//ww w . j a v a2 s .c om * * @return the string representation of the response * * @throws B2ApiException if there was an error with the call, most notably * a non OK status code (i.e. not 200) * @throws IOException if there was an error communicating with the API service */ protected CloseableHttpResponse executePost(HttpEntity entity) throws B2ApiException, IOException { URI uri = this.buildUri(); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = this.execute(httpPost); switch (httpResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return httpResponse; } throw new B2ApiException(EntityUtils.toString(httpResponse.getEntity()), new HttpResponseException( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase())); }