Skip to content

Instantly share code, notes, and snippets.

@rivol
Last active Nov 29, 2017
Embed
What would you like to do?
get retrofit error response's body
package com.gateme3.app.util;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import com.gateme3.app.db.PurchasedItem;
import com.gateme3.app.db.Venue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import retrofit.client.Response;
import retrofit.mime.MimeUtil;
import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
public class Util {
private static final String TAG = "GateMe";
private static final int BUFFER_SIZE = 0x1000;
public static String formatDateString(String dateString) {
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); //2011-08-08 20:00:00, 2013-10-20T19:00:00Z
Date date = null;
try {
date = inFormat.parse(dateString);
} catch (java.text.ParseException e) {
Log.e(TAG, "dateString ParseException", e);
} catch (NullPointerException e) {
Log.e(TAG, "dateString NullPointerException", e);
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEEEEEE dd.MM.yyyy HH:mm"); //2011-08-08 20:00:00
return outFormat.format(date);
}
public static String getDistanceString(Location me, Location point) {
int meters = (int) me.distanceTo(point);
if (meters < 1000) {
return meters + " meters";
} else {
float decMeters = meters;
float km = decMeters / 1000;
DecimalFormat df = new DecimalFormat("0.##");
return df.format(km) + " KM";
}
}
public static Location getLastKnownLoc(LocationManager lm) {
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i = providers.size() - 1; i >= 0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
return l;
}
public static String formatAddressString(Venue venue) {
return venue.street + " " + venue.city + ", " + venue.country;
}
public static String extractErrorString(String error) {
String[] parts = error.split("\\s+");
String formattedError = "";
for (int i = 3; i < parts.length; i++) {
formattedError = formattedError + parts[i] + " ";
}
return formattedError;
}
public static LinkedList<PurchasedItem> removeNonTickets(LinkedList<PurchasedItem> items) {
LinkedList<PurchasedItem> tickets = new LinkedList<PurchasedItem>();
for (int i = 0; i < items.size(); i++) {
if (items.get(i).type.equals("ticket")) {
tickets.add(items.get(i));
}
}
return items;
}
public static String getBodyString(Response response) throws IOException {
TypedInput body = response.getBody();
if (body!= null) {
if (!(body instanceof TypedByteArray)) {
// Read the entire response body to we can log it and replace the original response
response = readBodyToBytesIfNecessary(response);
body = response.getBody();
}
byte[] bodyBytes = ((TypedByteArray) body).getBytes();
String bodyMime = body.mimeType();
String bodyCharset = MimeUtil.parseCharset(bodyMime);
return new String(bodyBytes, bodyCharset);
}
return null;
}
static Response readBodyToBytesIfNecessary (Response response) throws IOException {
TypedInput body = response.getBody();
if (body == null || body instanceof TypedByteArray) {
return response;
}
String bodyMime = body.mimeType();
byte[] bodyBytes = streamToBytes(body.in());
body = new TypedByteArray(bodyMime, bodyBytes);
return replaceResponseBody(response, body);
}
static Response replaceResponseBody(Response response, TypedInput body) {
return new Response(response.getStatus(), response.getReason(), response.getHeaders(), body);
}
static class SynchronousExecutor implements Executor {
@Override public void execute(Runnable runnable) {
runnable.run();
}
}
static byte[] streamToBytes(InputStream stream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (stream != null) {
byte[] buf = new byte[BUFFER_SIZE];
int r;
while ((r = stream.read(buf)) != -1) {
baos.write(buf, 0, r);
}
}
return baos.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment