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);
}
}
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;
}
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()));
}
}
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);
}
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;
}
Aggregations