public Throwable handleError(RetrofitError cause) { Response r = cause.getResponse(); if (r != null) { Log.d("Retrofix", "Reponse Status: " + r.getStatus() + " , Reason: " + r.getReason()); } return cause; }
@Override public Response execute(Request request) throws IOException { Response response = wrappedClient.execute(request); boolean gzipped = false; for (Header h : response.getHeaders()) { if (h.getName() != null && h.getName().toLowerCase().equals("content-encoding") && h.getValue() != null && h.getValue().toLowerCase().equals("gzip")) { gzipped = true; break; } } Response r = null; if (gzipped) { InputStream is = null; ByteArrayOutputStream bos = null; try { is = new BufferedInputStream(new GZIPInputStream(response.getBody().in())); bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } TypedByteArray body = new TypedByteArray(response.getBody().mimeType(), bos.toByteArray()); r = new Response( response.getUrl(), response.getStatus(), response.getReason(), response.getHeaders(), body); } finally { if (is != null) { is.close(); } if (bos != null) { bos.close(); } } } else { r = response; } return r; }
/** * handles the retrofit errors<br> * (which is always thrown when http status != 200)<br> * and print some error msg * * @param re the Retrofit error */ private void handleRetrofitError(RetrofitError re) { Response r = re.getResponse(); String msg = ""; if (r != null) { msg = r.getStatus() + " " + r.getReason(); if (r.getBody() != null && r.getBody().length() > 0) { try { InputStream in = r.getBody().in(); String body = " - " + IOUtils.toString(in, "UTF-8"); in.close(); LOGGER.trace(body); } catch (IOException e1) { LOGGER.warn("IOException on Trakt error", e1); } } } else { msg = re.getMessage(); } LOGGER.error("Trakt error (wrong settings?) " + msg); MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, msg, "Settings.trakttv")); }
@Override public void failure(RetrofitError error) { Response response = error.getResponse(); String responseMsg = null; // if request failed and either no response received or internal error occurred if (response == null) { switch (error.getKind()) { case NETWORK: responseMsg = getString(R.string.error_net); break; case CONVERSION: responseMsg = getString(R.string.error_conv); break; case UNEXPECTED: responseMsg = getString(R.string.error_unknown); break; default: break; } mListener.onError(/*null,*/ responseMsg); return; } // if response received, but is not valid JSON JsonObject body = null; try { body = (JsonObject) error.getBodyAs(JsonObject.class); } catch (Exception e) { Log.e(ConversionException.class.getName(), e.getLocalizedMessage()); // responseMsg = getString(R.string.error_conv); mListener.onError(response.getReason() + "\nError code: " + response.getStatus()); return; } // if response is valid JSON String[] errorInfo = getErrorInfo(body); mListener.onError(errorInfo[1] + "\nError code: " + errorInfo[0]); }