Example usage for com.squareup.okhttp ResponseBody charStream

List of usage examples for com.squareup.okhttp ResponseBody charStream

Introduction

In this page you can find the example usage for com.squareup.okhttp ResponseBody charStream.

Prototype

public final Reader charStream() throws IOException 

Source Link

Document

Returns the response as a character stream decoded with the charset of the Content-Type header.

Usage

From source file:com.cliff.hsj.api.common.GsonConverter.java

License:Apache License

@Override
public T fromBody(ResponseBody body) throws IOException {
    Reader in = body.charStream();
    try {/*w  w w  . ja v a2 s .co m*/
        return typeAdapter.fromJson(in);
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:com.example.gitnb.api.retrofit.converter.GsonResponseBodyConverter.java

License:Apache License

@Override
public T convert(ResponseBody value) throws IOException {
    Reader reader = value.charStream();
    try {//from w  ww. ja v  a  2s .  c om
        return adapter.fromJson(reader);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:com.kubeiwu.easyandroid.easyhttp.core.retrofit.GsonConverter.java

License:Apache License

@Override
public T fromBody(ResponseBody body) throws IOException {
    Reader reader = body.charStream();
    try {/* w  w w. j  a  v  a  2 s  . c o m*/
        return typeAdapter.fromJson(reader);
    } finally {
        closeQuietly(reader);
    }
}

From source file:com.magnet.max.android.rest.marshalling.MagnetGsonConverter.java

License:Apache License

@Override
public T fromBody(ResponseBody responseBody) throws IOException {
    if (GsonDecorator.getInstance().isVoidType(typeToken.getType())) {
        return null;
    }//  w ww .jav a2s .c om

    if (typeToken.getRawType().equals(ResponseBody.class)) {
        return (T) responseBody;
    }

    if (GsonDecorator.getInstance().isByteArray(typeToken)) {
        return (T) responseBody.bytes();
    }

    //Charset charset = UTF_8;
    //if (responseBody.contentType() != null) {
    //  charset = responseBody.contentType().charset(charset);
    //}

    if (GsonDecorator.getInstance().isSimpleType(typeToken)) {
        return (T) GsonDecorator.getInstance().unmarshalSimpleType(responseBody.string(), typeToken.getType());
    } else {
        Reader in = responseBody.charStream();
        try {
            return (T) GsonDecorator.getInstance().fromJson(responseBody.charStream(), typeToken); //typeAdapter.fromJson(in);
        } catch (Exception e) {
            Log.e(TAG, "Error in fromBody \n" + e.getMessage());
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:com.nbs.ppm.contactapp.converter.GsonResponseBodyConverter.java

License:Apache License

@Override
public T convert(ResponseBody value) throws IOException {
    Reader reader = value.charStream();
    try {/*from   w ww. ja v  a2 s  .c o m*/
        return adapter.fromJson(reader);
    } finally {
        Util.closeQuietly(reader);
    }
}

From source file:com.thebluealliance.androidclient.datafeed.retrofit.LenientGsonConverter.java

License:Apache License

@Override
public T fromBody(ResponseBody body) throws IOException {
    Reader in = body.charStream();

    try {//from w ww .  j a va  2s. c  om
        JsonReader reader = new JsonReader(in);
        reader.setLenient(true);
        return typeAdapter.read(reader);
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:com.yandex.disk.rest.RestClientIO.java

License:Apache License

private <T> T parseJson(Response response, Class<T> classOfT) throws IOException {
    ResponseBody responseBody = null;
    try {/*from  www .ja  va2  s  .  c  o  m*/
        responseBody = response.body();
        Gson gson = new Gson();
        return gson.fromJson(responseBody.charStream(), classOfT);
    } finally {
        if (responseBody != null) {
            responseBody.close();
        }
    }
}

From source file:feign.okhttp.OkHttpClient.java

License:Apache License

private static feign.Response.Body toBody(final ResponseBody input) {
    if (input == null || input.contentLength() == 0) {
        return null;
    }/*from w  w w .java 2  s  .  co m*/
    if (input.contentLength() > Integer.MAX_VALUE) {
        throw new UnsupportedOperationException("Length too long " + input.contentLength());
    }
    final Integer length = input.contentLength() != -1 ? (int) input.contentLength() : null;

    return new feign.Response.Body() {

        @Override
        public void close() throws IOException {
            input.close();
        }

        @Override
        public Integer length() {
            return length;
        }

        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public InputStream asInputStream() throws IOException {
            return input.byteStream();
        }

        @Override
        public Reader asReader() throws IOException {
            return input.charStream();
        }
    };
}

From source file:retrofit.GsonResponseBodyConverter.java

License:Apache License

@Override
public T convert(ResponseBody value) throws IOException {
    Reader reader = value.charStream();
    try {/*from w  w w .  j  ava  2s  .co m*/
        return gson.fromJson(reader, type);
    } finally {
        Utils.closeQuietly(reader);
    }
}

From source file:retrofit.JacksonResponseBodyConverter.java

License:Apache License

@Override
public T convert(ResponseBody value) throws IOException {
    Reader reader = value.charStream();
    try {/*  w ww .ja v  a  2s . com*/
        return adapter.readValue(reader);
    } finally {
        Utils.closeQuietly(reader);
    }
}