Search in sources :

Example 56 with Dispatcher

use of okhttp3.Dispatcher in project okhttp by square.

the class Benchmark method startServer.

private MockWebServer startServer() throws IOException {
    Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
    MockWebServer server = new MockWebServer();
    if (tls) {
        SslClient sslClient = SslClient.localhost();
        server.useHttps(sslClient.socketFactory, false);
        server.setProtocols(protocols);
    }
    final MockResponse response = newResponse();
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            return response;
        }
    });
    server.start();
    return server;
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) Dispatcher(okhttp3.mockwebserver.Dispatcher)

Example 57 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 58 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 59 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)

Example 60 with Dispatcher

use of okhttp3.Dispatcher in project instructure-android by instructure.

the class CanvasRestAdapter method buildRollCallAdapter.

public Retrofit buildRollCallAdapter(@NonNull String url) {
    final Gson gson = new GsonBuilder().setLenient().create();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(DEBUG ? HttpLoggingInterceptor.Level.HEADERS : HttpLoggingInterceptor.Level.NONE);
    return new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).client(new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(new RollCallInterceptor()).readTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS).dispatcher(mDispatcher).build()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Aggregations

Dispatcher (okhttp3.Dispatcher)40 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Dispatcher (okhttp3.mockwebserver.Dispatcher)24 IOException (java.io.IOException)17 Before (org.junit.Before)14 Test (org.junit.Test)13 Call (okhttp3.Call)11 MockWebServer (okhttp3.mockwebserver.MockWebServer)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 Interceptor (okhttp3.Interceptor)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4