Search in sources :

Example 96 with OkHttpClient

use of okhttp3.OkHttpClient in project buck by facebook.

the class SingleUriServiceTest method testClientIsCalledWithFullUrl.

@Test
public void testClientIsCalledWithFullUrl() throws IOException, InterruptedException {
    OkHttpClient mockClient = EasyMock.createMock(OkHttpClient.class);
    String path = "my/super/path";
    Request.Builder request = new Request.Builder().url(SERVER + path).get();
    Call mockCall = EasyMock.createMock(Call.class);
    EasyMock.expect(mockClient.newCall(EasyMock.anyObject(Request.class))).andReturn(mockCall);
    Response response = new Response.Builder().message("my super response").request(request.build()).protocol(Protocol.HTTP_1_1).code(200).build();
    EasyMock.expect(mockCall.execute()).andReturn(response);
    EasyMock.replay(mockCall, mockClient);
    try (SingleUriService service = new SingleUriService(SERVER, mockClient)) {
        service.makeRequest(path, request);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 97 with OkHttpClient

use of okhttp3.OkHttpClient in project fresco by facebook.

the class ImagePipelineConfigFactory method getOkHttpImagePipelineConfig.

/**
   * Creates config using OkHttp as network backed.
   */
public static ImagePipelineConfig getOkHttpImagePipelineConfig(Context context) {
    if (sOkHttpImagePipelineConfig == null) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();
        ImagePipelineConfig.Builder configBuilder = OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient);
        configureCaches(configBuilder, context);
        configureLoggingListeners(configBuilder);
        sOkHttpImagePipelineConfig = configBuilder.build();
    }
    return sOkHttpImagePipelineConfig;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor)

Example 98 with OkHttpClient

use of okhttp3.OkHttpClient in project buck by facebook.

the class DoctorReportHelper method uploadRequest.

public DoctorEndpointResponse uploadRequest(DoctorEndpointRequest request) {
    if (!doctorConfig.getEndpointUrl().isPresent()) {
        String errorMsg = String.format("Doctor endpoint URL is not set. Please set [%s] %s on your .buckconfig", DoctorConfig.DOCTOR_SECTION, DoctorConfig.URL_FIELD);
        return createErrorDoctorEndpointResponse(errorMsg);
    }
    byte[] requestJson;
    try {
        requestJson = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse("Failed to encode request to JSON. " + "Reason: " + e.getMessage());
    }
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).readTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).writeTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).build();
    Response httpResponse;
    try {
        RequestBody requestBody;
        ImmutableMap<String, String> extraArgs = doctorConfig.getExtraRequestArgs();
        if (extraArgs.isEmpty()) {
            requestBody = RequestBody.create(JSON, requestJson);
        } else {
            FormBody.Builder formBody = new FormBody.Builder();
            formBody.add("data", new String(requestJson));
            for (Map.Entry<String, String> entry : extraArgs.entrySet()) {
                formBody.add(entry.getKey(), entry.getValue());
            }
            requestBody = formBody.build();
        }
        Request httpRequest = new Request.Builder().url(doctorConfig.getEndpointUrl().get()).post(requestBody).build();
        httpResponse = httpClient.newCall(httpRequest).execute();
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse("Failed to perform the request to " + doctorConfig.getEndpointUrl().get() + ". Reason: " + e.getMessage());
    }
    try {
        if (httpResponse.isSuccessful()) {
            String body = new String(httpResponse.body().bytes(), Charsets.UTF_8);
            return objectMapper.readValue(body, DoctorEndpointResponse.class);
        }
        return createErrorDoctorEndpointResponse("Request was not successful.");
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) FormBody(okhttp3.FormBody) DoctorEndpointRequest(com.facebook.buck.doctor.config.DoctorEndpointRequest) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) DoctorEndpointResponse(com.facebook.buck.doctor.config.DoctorEndpointResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RequestBody(okhttp3.RequestBody)

Example 99 with OkHttpClient

use of okhttp3.OkHttpClient in project okhttputils by hongyangAndroid.

the class MyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));
    HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
    //        CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10000L, TimeUnit.MILLISECONDS).readTimeout(10000L, TimeUnit.MILLISECONDS).addInterceptor(new LoggerInterceptor("TAG")).cookieJar(cookieJar1).hostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).build();
    OkHttpUtils.initClient(okHttpClient);
}
Also used : ClearableCookieJar(com.franmontiel.persistentcookiejar.ClearableCookieJar) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(com.zhy.http.okhttp.log.LoggerInterceptor) PersistentCookieJar(com.franmontiel.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.franmontiel.persistentcookiejar.cache.SetCookieCache) SSLSession(javax.net.ssl.SSLSession) SharedPrefsCookiePersistor(com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor) HttpsUtils(com.zhy.http.okhttp.https.HttpsUtils) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 100 with OkHttpClient

use of okhttp3.OkHttpClient in project ListenerMusicPlayer by hefuyicoder.

the class NetworkModule method provideLastFMRetrofit.

@Provides
@Named("lastfm")
@PerApplication
Retrofit provideLastFMRetrofit() {
    String endpointUrl = Constants.BASE_API_URL_LASTFM;
    Gson gson = new GsonBuilder().create();
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(FileUtil.getHttpCacheDir(ListenerApp.getContext()), Constants.HTTP_CACHE_SIZE)).connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(Constants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS).build();
    //        OkHttpClient newClient = client.newBuilder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(endpointUrl).client(client).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit;
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Named(javax.inject.Named) PerApplication(io.hefuyi.listener.injector.scope.PerApplication) Provides(dagger.Provides)

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