Search in sources :

Example 1 with TypedInput

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);
}
Also used : InputStream(java.io.InputStream) TypedInput(retrofit.mime.TypedInput) ArrayList(java.util.ArrayList) Response(retrofit.client.Response) HttpURLConnection(java.net.HttpURLConnection) Header(retrofit.client.Header) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with TypedInput

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TypedInput(retrofit.mime.TypedInput) FileNotFoundException(java.io.FileNotFoundException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) FileInputStream(java.io.FileInputStream) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) CacheCreationException(com.octo.android.robospice.persistence.exception.CacheCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException)

Aggregations

InputStream (java.io.InputStream)2 TypedInput (retrofit.mime.TypedInput)2 CacheCreationException (com.octo.android.robospice.persistence.exception.CacheCreationException)1 CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)1 CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Header (retrofit.client.Header)1 Response (retrofit.client.Response)1