Search in sources :

Example 1 with TypedOutput

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());
    }
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) Header(retrofit.client.Header) TypedOutput(retrofit.mime.TypedOutput)

Example 2 with TypedOutput

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();
        }
    }
}
Also used : TypedOutput(retrofit.mime.TypedOutput) FileOutputStream(java.io.FileOutputStream)

Aggregations

TypedOutput (retrofit.mime.TypedOutput)2 FileOutputStream (java.io.FileOutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 Header (retrofit.client.Header)1