use of retrofit.mime.TypedInput in project enroscar by stanfy.
the class RetrofitClient method readResponse.
private Response readResponse(URLConnection connection) throws IOException {
int status = HttpURLConnection.HTTP_OK;
String reason = "";
if (connection instanceof HttpURLConnection) {
status = ((HttpURLConnection) connection).getResponseCode();
reason = ((HttpURLConnection) connection).getResponseMessage();
}
List<Header> headers = new ArrayList<>();
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
String name = field.getKey();
for (String value : field.getValue()) {
headers.add(new Header(name, value));
}
}
String mimeType = connection.getContentType();
int length = connection.getContentLength();
InputStream stream;
if (status >= 400 && connection instanceof HttpURLConnection) {
stream = ((HttpURLConnection) connection).getErrorStream();
} else {
stream = connection.getInputStream();
}
TypedInput responseBody = new TypedInputStream(mimeType, length, stream);
return new Response(connection.getURL().toString(), status, reason, headers, responseBody);
}
use of retrofit.mime.TypedInput in project robospice by stephanenicolas.
the class RetrofitObjectPersister method readCacheDataFromFile.
@SuppressWarnings("unchecked")
@Override
protected T readCacheDataFromFile(File file) throws CacheLoadingException {
InputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
final byte[] body = IOUtils.toByteArray(fileInputStream);
TypedInput typedInput = new TypedInput() {
@Override
public String mimeType() {
return "application/json";
}
@Override
public long length() {
return body.length;
}
@Override
public InputStream in() throws IOException {
return new ByteArrayInputStream(body);
}
};
return (T) converter.fromBody(typedInput, getHandledClass());
} catch (FileNotFoundException e) {
// Should not occur (we test before if file exists)
// Do not throw, file is not cached
Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
return null;
} catch (Exception e) {
throw new CacheLoadingException(e);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
}
Aggregations