Search in sources :

Example 16 with Dispatcher

use of okhttp3.Dispatcher in project okhttp by square.

the class CallTest method cancelInFlightBeforeResponseReadThrowsIOE.

@Test
public void cancelInFlightBeforeResponseReadThrowsIOE() throws Exception {
    Request request = new Request.Builder().url(server.url("/a")).build();
    final Call call = client.newCall(request);
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            call.cancel();
            return new MockResponse().setBody("A");
        }
    });
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 17 with Dispatcher

use of okhttp3.Dispatcher in project okhttp by square.

the class InterceptorTest method networkInterceptorReturnsNull.

@Test
public void networkInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            chain.proceed(chain.request());
            return null;
        }
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertEquals("interceptor " + interceptor + " returned null", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 18 with Dispatcher

use of okhttp3.Dispatcher in project okhttp by square.

the class InterceptorTest method interceptorThrowsRuntimeExceptionAsynchronous.

/**
   * When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
   * exception goes to the uncaught exception handler.
   */
private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) throws Exception {
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);
    assertEquals("boom!", executor.takeException().getMessage());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 19 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class OnDownloadListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(final Response response, FileCard fileCard) throws Throwable {
    ResponseBody body = response.body();
    if (body == null) {
        throw new NullPointerException("response body is null");
    } else {
        final Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        InputStream is = body.byteStream();
        long contentLength = body.contentLength();
        final File file = FileUtils.saveFile(is, contentLength, fileCard, new OnSaveListener() {

            @Override
            public void OnProgress(final long progress, final long total) {
                dispatcher.dispatchToUIThread(new Runnable() {

                    @Override
                    public void run() {
                        onProgressChanged(progress, total);
                    }
                });
            }
        });
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess((T) file);
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) Dispatcher(io.chelizi.amokhttp.Dispatcher) File(java.io.File) ResponseBody(okhttp3.ResponseBody)

Example 20 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class OnFindListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    String responseStr = null;
    if (responseBody != null) {
        responseStr = responseBody.string();
    }
    Type type = ClassUtils.getType(OnFindListener.this.getClass());
    T bean = null;
    if (type != null) {
        if (TextUtils.equals(type.toString(), "class java.lang.String"))
            bean = (T) responseStr;
        else
            bean = new Gson().fromJson(responseStr, type);
    }
    final T finalBean = bean;
    Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
    dispatcher.dispatchToUIThread(new Runnable() {

        @Override
        public void run() {
            onResponseSuccess(finalBean);
        }
    });
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)9 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 Dispatcher (okhttp3.Dispatcher)8 OkHttpClient (okhttp3.OkHttpClient)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 ResponseBody (okhttp3.ResponseBody)6 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 Request (okhttp3.Request)5 Gson (com.google.gson.Gson)4 Dispatcher (io.chelizi.amokhttp.Dispatcher)4 Response (okhttp3.Response)4 Type (java.lang.reflect.Type)3 Call (okhttp3.Call)2 Interceptor (okhttp3.Interceptor)2 Buffer (okio.Buffer)2 Test (org.testng.annotations.Test)2 Bundle (android.os.Bundle)1 Message (android.os.Message)1 BuckConfig (com.facebook.buck.cli.BuckConfig)1