use of com.squareup.okhttp.OkHttpClient in project dropbox-sdk-java by dropbox.
the class ITUtil method newOkHttpRequestor.
public static OkHttpRequestor newOkHttpRequestor() {
OkHttpClient httpClient = OkHttpRequestor.defaultOkHttpClient().clone();
httpClient.setReadTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
return new OkHttpRequestor(httpClient);
}
use of com.squareup.okhttp.OkHttpClient in project SimplifyReader by chentao0707.
the class OkHttpStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
OkHttpClient client = mClient.clone();
int timeoutMs = request.getTimeoutMs();
client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
for (final String name : additionalHeaders.keySet()) {
okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for (int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
use of com.squareup.okhttp.OkHttpClient in project android-rest-client by darko1002001.
the class RestClientRequest method runRequest.
protected void runRequest() {
if (handler == null) {
handler = new UIThreadResponseHandler<>(callback);
}
if (statusHandler == null) {
statusHandler = new DefaultResponseStatusHandler();
}
OkHttpClient client = generateClient();
StringBuilder url = new StringBuilder(this.url);
StringBuilder queryParams = this.queryParams;
if (queryParams != null) {
url.append(queryParams);
}
request.url(url.toString());
if (this.authenticationProvider != null) {
authenticationProvider.authenticateRequest(this);
}
Request preparedRequest = request.build();
Response response;
try {
response = client.newCall(preparedRequest).execute();
} catch (final Exception e) {
ResponseStatus responseStatus = ResponseStatus.getConnectionErrorStatus();
Log.e(TAG, responseStatus.toString(), e);
handler.handleError(responseStatus);
doAfterRunRequestInBackgroundThread();
return;
}
final ResponseStatus status = new ResponseStatus(response.code(), response.message());
Log.d(TAG, status.toString());
if (handleResponseStatus(status)) {
doAfterRunRequestInBackgroundThread();
return;
}
try {
if (parser != null) {
InputStream instream = response.body().byteStream();
final T responseData = parser.parse(instream);
close(response.body());
doAfterSuccessfulRequestInBackgroundThread(responseData);
handler.handleSuccess(responseData, status);
} else {
Log.i(TAG, "You haven't added a parser for your request");
handler.handleSuccess(null, status);
doAfterSuccessfulRequestInBackgroundThread(null);
}
} catch (final Exception e) {
ResponseStatus responseStatus = ResponseStatus.getParseErrorStatus();
Log.d(TAG, responseStatus.toString(), e);
handler.handleError(responseStatus);
}
doAfterRunRequestInBackgroundThread();
}
use of com.squareup.okhttp.OkHttpClient in project android-rest-client by darko1002001.
the class RestClientRequest method generateClient.
private OkHttpClient generateClient() {
OkHttpClient client = new OkHttpClient();
customizeClient(client);
return client;
}
use of com.squareup.okhttp.OkHttpClient in project stetho by facebook.
the class StethoInterceptorTest method setUp.
@Before
public void setUp() {
PowerMockito.mockStatic(NetworkEventReporterImpl.class);
mMockEventReporter = Mockito.mock(NetworkEventReporter.class);
Mockito.when(mMockEventReporter.isEnabled()).thenReturn(true);
PowerMockito.when(NetworkEventReporterImpl.get()).thenReturn(mMockEventReporter);
mInterceptor = new StethoInterceptor();
mClientWithInterceptor = new OkHttpClient();
mClientWithInterceptor.networkInterceptors().add(mInterceptor);
}
Aggregations