Search in sources :

Example 51 with OkHttpClient

use of okhttp3.OkHttpClient in project twitter4j by yusuke.

the class Http2ClientTest method testHttp2.

public void testHttp2() throws Exception {
    AlternativeHttpClientImpl.sPreferSpdy = false;
    AlternativeHttpClientImpl.sPreferHttp2 = true;
    AlternativeHttpClientImpl http = callOembed();
    // check HTTP/2.0
    Field f = http.getClass().getDeclaredField("okHttpClient");
    f.setAccessible(true);
    OkHttpClient client = (OkHttpClient) f.get(http);
    assertNotNull("ensure that OkHttpClient is used", client);
    ConnectionPool p = client.connectionPool();
    assertEquals(1, p.connectionCount());
    assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Field(java.lang.reflect.Field) OkHttpClient(okhttp3.OkHttpClient)

Example 52 with OkHttpClient

use of okhttp3.OkHttpClient in project twitter4j by yusuke.

the class Http2ClientTest method testNoSpdy.

public void testNoSpdy() throws Exception {
    AlternativeHttpClientImpl.sPreferSpdy = false;
    AlternativeHttpClientImpl.sPreferHttp2 = false;
    AlternativeHttpClientImpl http = callOembed();
    // check not SPDY
    Field f = http.getClass().getDeclaredField("okHttpClient");
    f.setAccessible(true);
    OkHttpClient client = (OkHttpClient) f.get(http);
    ConnectionPool p = client.connectionPool();
    assertEquals(1, p.connectionCount());
    assertEquals(Protocol.HTTP_1_1, http.getLastRequestProtocol());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Field(java.lang.reflect.Field) OkHttpClient(okhttp3.OkHttpClient)

Example 53 with OkHttpClient

use of okhttp3.OkHttpClient in project YourAppIdea by Michenux.

the class MongolabPlaceServiceFactory method create.

public static MongolabPlaceService create(Context context) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer());
    gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
    String url = context.getString(R.string.aroundme_placeremoteprovider_url);
    Retrofit retrofit = new Retrofit.Builder().baseUrl(url).client(client).addConverterFactory(GsonConverterFactory.create(gsonBuilder.create())).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit.create(MongolabPlaceService.class);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) LoggingInterceptor(org.michenux.drodrolib.network.okhttp.LoggingInterceptor) TimestampDeserializer(org.michenux.drodrolib.network.gson.TimestampDeserializer) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) LocationDeserializer(org.michenux.drodrolib.network.gson.LocationDeserializer)

Example 54 with OkHttpClient

use of okhttp3.OkHttpClient in project OkHttp3 by MrZhousf.

the class DownUpLoadHelper method downloadFile.

/**
     * 文件下载
     */
void downloadFile(final OkHttpHelper helper) {
    try {
        final HttpInfo info = httpInfo;
        final DownloadFileInfo fileInfo = helper.getDownloadFileInfo();
        String url = fileInfo.getUrl();
        if (TextUtils.isEmpty(url)) {
            showLog("下载文件失败:文件下载地址不能为空!");
            return;
        }
        info.setUrl(url);
        ProgressCallback progressCallback = fileInfo.getProgressCallback();
        //获取文件断点
        long completedSize = fetchCompletedSize(fileInfo);
        fileInfo.setCompletedSize(completedSize);
        //添加下载任务
        if (null == downloadTaskMap)
            downloadTaskMap = new ConcurrentHashMap<>();
        if (downloadTaskMap.containsKey(fileInfo.getSaveFileNameEncrypt())) {
            showLog(fileInfo.getSaveFileName() + " 已在下载任务中");
            return;
        }
        downloadTaskMap.put(fileInfo.getSaveFileNameEncrypt(), fileInfo.getSaveFileNameEncrypt());
        Interceptor interceptor = new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), fileInfo, timeStamp, requestTag)).build();
            }
        };
        //采用新的OkHttpClient处理多线程干扰回调进度问题
        OkHttpClient httpClient = helper.getClientBuilder().addInterceptor(interceptor).build();
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(url).header("RANGE", "bytes=" + completedSize + "-");
        helper.getHttpHelper().addHeadsToRequest(info, requestBuilder);
        Request request = requestBuilder.build();
        helper.setRequest(request);
        helper.setHttpClient(httpClient);
        helper.getHttpHelper().responseCallback(helper.doRequestSync(), progressCallback, OkMainHandler.RESPONSE_DOWNLOAD_CALLBACK, requestTag);
        //删除下载任务
        if (null != downloadTaskMap) {
            downloadTaskMap.remove(fileInfo.getSaveFileNameEncrypt());
        }
    } catch (Exception e) {
        showLog("下载文件失败:" + e.getMessage());
    }
}
Also used : DownloadFileInfo(com.okhttplib.bean.DownloadFileInfo) OkHttpClient(okhttp3.OkHttpClient) ProgressCallback(com.okhttplib.callback.ProgressCallback) Request(okhttp3.Request) ProgressResponseBody(com.okhttplib.progress.ProgressResponseBody) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) HttpInfo(com.okhttplib.HttpInfo) Response(okhttp3.Response) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor)

Example 55 with OkHttpClient

use of okhttp3.OkHttpClient in project yyl_example by Relucent.

the class OkhttpTest2 method main.

public static void main(String[] args) throws IOException {
    OkHttpClient client;
    (client = //
    new OkHttpClient.Builder().build()).newCall(//
    new Request.Builder().url(//
    "https://www.baidu.com/").header("Connection", //close | keep-alive
    "close").get().build()).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            System.out.println(response.body().string());
        }

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }
    });
    //应用关闭时候需要关闭线程池
    client.dispatcher().executorService().shutdown();
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) IOException(java.io.IOException)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)149 Request (okhttp3.Request)73 Response (okhttp3.Response)61 IOException (java.io.IOException)52 Test (org.junit.Test)35 Call (okhttp3.Call)24 Retrofit (retrofit2.Retrofit)24 Interceptor (okhttp3.Interceptor)19 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)18 MockResponse (okhttp3.mockwebserver.MockResponse)18 File (java.io.File)15 GsonBuilder (com.google.gson.GsonBuilder)12 Provides (dagger.Provides)12 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 Gson (com.google.gson.Gson)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Cache (okhttp3.Cache)10 Observable (rx.Observable)10 Singleton (javax.inject.Singleton)9