use of okhttp3 in project fresco by facebook.
the class OkHttpNetworkFetcher method fetchWithRequest.
protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
final Call call = mCallFactory.newCall(request);
fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
if (Looper.myLooper() != Looper.getMainLooper()) {
call.cancel();
} else {
mCancellationExecutor.execute(new Runnable() {
@Override
public void run() {
call.cancel();
}
});
}
}
});
call.enqueue(new okhttp3.Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
fetchState.responseTime = SystemClock.elapsedRealtime();
final ResponseBody body = response.body();
try {
if (!response.isSuccessful()) {
handleException(call, new IOException("Unexpected HTTP code " + response), callback);
return;
}
long contentLength = body.contentLength();
if (contentLength < 0) {
contentLength = 0;
}
callback.onResponse(body.byteStream(), (int) contentLength);
} catch (Exception e) {
handleException(call, e, callback);
} finally {
try {
body.close();
} catch (Exception e) {
FLog.w(TAG, "Exception when closing response body", e);
}
}
}
@Override
public void onFailure(Call call, IOException e) {
handleException(call, e, callback);
}
});
}
use of okhttp3 in project okhttputils by hongyangAndroid.
the class OkHttpUtils method execute.
public void execute(final RequestCall requestCall, Callback callback) {
if (callback == null)
callback = Callback.CALLBACK_DEFAULT;
final Callback finalCallback = callback;
final int id = requestCall.getOkHttpRequest().getId();
requestCall.getCall().enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, final IOException e) {
sendFailResultCallback(call, e, finalCallback, id);
}
@Override
public void onResponse(final Call call, final Response response) {
try {
if (call.isCanceled()) {
sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id);
return;
}
if (!finalCallback.validateReponse(response, id)) {
sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id);
return;
}
Object o = finalCallback.parseNetworkResponse(response, id);
sendSuccessResultCallback(o, finalCallback, id);
} catch (Exception e) {
sendFailResultCallback(call, e, finalCallback, id);
} finally {
if (response.body() != null)
response.body().close();
}
}
});
}
use of okhttp3 in project retrofit by square.
the class CallTest method conversionProblemIncomingMaskedByConverterIsUnwrapped.
@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.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.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();
}
}).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");
}
}
use of okhttp3 in project retrofit by square.
the class OkHttpCall method parseResponse.
Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
ResponseBody rawBody = rawResponse.body();
// Remove the body's source (the only stateful object) so we can pass the response along.
rawResponse = rawResponse.newBuilder().body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength())).build();
int code = rawResponse.code();
if (code < 200 || code >= 300) {
try {
// Buffer the entire body to avoid future I/O.
ResponseBody bufferedBody = Utils.buffer(rawBody);
return Response.error(bufferedBody, rawResponse);
} finally {
rawBody.close();
}
}
if (code == 204 || code == 205) {
rawBody.close();
return Response.success(null, rawResponse);
}
ExceptionCatchingRequestBody catchingBody = new ExceptionCatchingRequestBody(rawBody);
try {
T body = serviceMethod.toResponse(catchingBody);
return Response.success(body, rawResponse);
} catch (RuntimeException e) {
// If the underlying source threw an exception, propagate that rather than indicating it was
// a runtime exception.
catchingBody.throwIfCaught();
throw e;
}
}
use of okhttp3 in project retrofit by square.
the class RequestBuilderTest method headerParamList.
@Test
public void headerParamList() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@Header("foo") List<String> kit) {
return null;
}
}
Request request = buildRequest(Example.class, Arrays.asList("bar", null, "baz"));
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(2);
assertThat(headers.values("foo")).containsExactly("bar", "baz");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
Aggregations