List of usage examples for com.squareup.okhttp ResponseBody source
public abstract BufferedSource source() throws IOException;
From source file:retrofit.MoshiConverter.java
License:Apache License
@Override public T fromBody(ResponseBody body) throws IOException { BufferedSource source = body.source(); try {//w ww.j av a 2s . c om return adapter.fromJson(source); } finally { try { source.close(); } catch (IOException ignored) { } } }
From source file:retrofit.MoshiResponseBodyConverter.java
License:Apache License
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try {/*from w w w.j ava 2 s .com*/ return adapter.fromJson(source); } finally { Utils.closeQuietly(source); } }
From source file:retrofit.WireResponseBodyConverter.java
License:Apache License
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try {/* w w w . java 2 s. c o m*/ return wire.parseFrom(source, cls); } finally { Utils.closeQuietly(source); } }
From source file:retrofit2.CallTest.java
License:Apache License
@Test public void conversionProblemIncomingMaskedByConverterIsUnwrapped() throws IOException { // MWS has no way to trigger IOExceptions during the response body so use an interceptor. OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor() { @Override// w ww . j ava2 s . c o m public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { com.squareup.okhttp.Response response = chain.proceed(chain.request()); ResponseBody body = response.body(); BufferedSource source = Okio.buffer(new ForwardingSource(body.source()) { @Override public long read(Buffer sink, long byteCount) throws IOException { throw new IOException("cause"); } }); body = ResponseBody.create(body.contentType(), body.contentLength(), source); return response.newBuilder().body(body).build(); } }); Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client) .addConverterFactory(new ToStringConverterFactory() { @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return new Converter<ResponseBody, String>() { @Override public String convert(ResponseBody value) throws IOException { try { return value.string(); } catch (IOException e) { // Some serialization libraries mask transport problems in runtime exceptions. Bad! throw new RuntimeException("wrapper", e); } } }; } }).build(); Service example = retrofit.create(Service.class); server.enqueue(new MockResponse().setBody("Hi")); Call<String> call = example.getString(); try { call.execute(); fail(); } catch (IOException e) { assertThat(e).hasMessage("cause"); } }
From source file:retrofit2.WireResponseBodyConverter.java
License:Apache License
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = null;//from ww w.jav a 2 s. com try { source = value.source(); return adapter.decode(source); } finally { if (source != null) { try { source.close(); } catch (IOException ignored) { } } } }