use of retrofit.mime.TypedOutput in project enroscar by stanfy.
the class RetrofitClient method prepareRequest.
private void prepareRequest(final URLConnection connection, final Request request) throws IOException {
if (connection instanceof HttpURLConnection) {
// HttpURLConnection artificially restricts request method
try {
((HttpURLConnection) connection).setRequestMethod(request.getMethod());
} catch (ProtocolException e) {
try {
methodField.set(connection, request.getMethod());
} catch (IllegalAccessException e1) {
throw RetrofitError.unexpectedError(getUrl(request), e1);
}
}
}
connection.setDoInput(true);
for (Header header : request.getHeaders()) {
connection.addRequestProperty(header.getName(), header.getValue());
}
TypedOutput body = request.getBody();
if (body != null) {
connection.setDoOutput(true);
connection.addRequestProperty("Content-Type", body.mimeType());
long length = body.length();
if (length != -1) {
connection.addRequestProperty("Content-Length", String.valueOf(length));
}
if (connection instanceof HttpURLConnection) {
if (length != -1) {
((HttpURLConnection) connection).setFixedLengthStreamingMode((int) length);
} else {
((HttpURLConnection) connection).setChunkedStreamingMode(CHUNK_SIZE);
}
}
body.writeTo(connection.getOutputStream());
}
}
use of retrofit.mime.TypedOutput in project robospice by stephanenicolas.
the class RetrofitObjectPersister method saveData.
private void saveData(T data, Object cacheKey) throws IOException, CacheSavingException {
// transform the content in json to store it in the cache
TypedOutput typedBytes = converter.toBody(data);
FileOutputStream out = null;
try {
out = new FileOutputStream(getCacheFile(cacheKey));
typedBytes.writeTo(out);
} finally {
if (out != null) {
out.close();
}
}
}
Aggregations