Search in sources :

Example 11 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class OnAddListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnAddListener.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 12 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class OnUploadListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnUploadListener.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 13 with Dispatcher

use of okhttp3.Dispatcher in project Tusky by Vavassor.

the class BaseActivity method createMastodonAPI.

protected void createMastodonAPI() {
    mastodonApiDispatcher = new Dispatcher();
    Gson gson = new GsonBuilder().registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()).registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()).create();
    OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request.Builder builder = originalRequest.newBuilder();
            String accessToken = getAccessToken();
            if (accessToken != null) {
                builder.header("Authorization", String.format("Bearer %s", accessToken));
            }
            Request newRequest = builder.build();
            return chain.proceed(newRequest);
        }
    }).dispatcher(mastodonApiDispatcher).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl()).client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    mastodonAPI = retrofit.create(MastodonAPI.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) SpannedTypeAdapter(com.keylesspalace.tusky.json.SpannedTypeAdapter) Request(okhttp3.Request) Gson(com.google.gson.Gson) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) StringWithEmojiTypeAdapter(com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter) MastodonAPI(com.keylesspalace.tusky.network.MastodonAPI) Interceptor(okhttp3.Interceptor) StringWithEmoji(com.keylesspalace.tusky.json.StringWithEmoji)

Example 14 with Dispatcher

use of okhttp3.Dispatcher in project retrofit by square.

the class Crawler method main.

public static void main(String... args) throws Exception {
    Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
    dispatcher.setMaxRequests(20);
    dispatcher.setMaxRequestsPerHost(1);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(HttpUrl.parse("https://example.com/")).addConverterFactory(PageAdapter.FACTORY).client(okHttpClient).build();
    PageService pageService = retrofit.create(PageService.class);
    Crawler crawler = new Crawler(pageService);
    crawler.crawlPage(HttpUrl.parse(args[0]));
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher)

Example 15 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)

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