Search in sources :

Example 21 with OkHttpClient

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);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) OkHttpRequestor(com.dropbox.core.http.OkHttpRequestor)

Example 22 with OkHttpClient

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;
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Headers(com.squareup.okhttp.Headers) Request(com.android.volley.Request) BasicStatusLine(org.apache.http.message.BasicStatusLine) Response(com.squareup.okhttp.Response) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHeader(org.apache.http.message.BasicHeader)

Example 23 with OkHttpClient

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();
}
Also used : Response(com.squareup.okhttp.Response) DefaultResponseStatusHandler(com.dg.libs.rest.handlers.DefaultResponseStatusHandler) OkHttpClient(com.squareup.okhttp.OkHttpClient) ResponseStatus(com.dg.libs.rest.domain.ResponseStatus) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) HttpRequest(com.dg.libs.rest.HttpRequest) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with OkHttpClient

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;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient)

Example 25 with OkHttpClient

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);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) NetworkEventReporter(com.facebook.stetho.inspector.network.NetworkEventReporter) Before(org.junit.Before)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2