Search in sources :

Example 41 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project pictureapp by EyeSeeTea.

the class BasicAuthenticator method executeCall.

/**
     * Call to DHIS Server
     */
static Response executeCall(JSONObject data, String url, String method) throws IOException {
    final String DHIS_URL = url;
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    Request.Builder builder = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);
    switch(method) {
        case "POST":
            RequestBody postBody = RequestBody.create(JSON, data.toString());
            builder.post(postBody);
            break;
        case "PUT":
            RequestBody putBody = RequestBody.create(JSON, data.toString());
            builder.put(putBody);
            break;
        case "PATCH":
            RequestBody patchBody = RequestBody.create(JSON, data.toString());
            builder.patch(patchBody);
            break;
        case "GET":
            builder.get();
            break;
    }
    Request request = builder.build();
    return client.newCall(request).execute();
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) RequestBody(com.squareup.okhttp.RequestBody)

Example 42 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project weex-example by KalicyZhou.

the class DefaultWebSocketAdapter method connect.

@Override
public void connect(String url, @Nullable String protocol, EventListener listener) {
    this.eventListener = listener;
    OkHttpClient okHttpClient = new OkHttpClient();
    Request.Builder builder = new Request.Builder();
    if (protocol != null) {
        builder.addHeader(HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
    }
    builder.url(url);
    WebSocketCall.create(okHttpClient, builder.build()).enqueue(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
            ws = webSocket;
            eventListener.onOpen();
        }

        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            eventListener.onMessage(payload.readUtf8());
            payload.close();
        }

        @Override
        public void onPong(Buffer payload) {
        }

        @Override
        public void onClose(int code, String reason) {
            eventListener.onClose(code, reason, true);
        }

        @Override
        public void onFailure(IOException e) {
            e.printStackTrace();
            if (e instanceof EOFException) {
                eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
            } else {
                eventListener.onError(e.getMessage());
            }
        }
    });
}
Also used : Buffer(okio.Buffer) WebSocketListener(com.squareup.okhttp.ws.WebSocketListener) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) WebSocket(com.squareup.okhttp.ws.WebSocket) Response(com.squareup.okhttp.Response) EOFException(java.io.EOFException) BufferedSource(okio.BufferedSource)

Example 43 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project weex-example by KalicyZhou.

the class HotRefreshManager method connect.

public boolean connect(String url) {
    OkHttpClient httpClient = new OkHttpClient();
    Request request = new Request.Builder().url(url).addHeader("sec-websocket-protocol", "echo-protocol").build();
    WebSocketCall.create(httpClient, request).enqueue(new WXWebSocketListener(url));
    return true;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request)

Example 44 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project bdcodehelper by boredream.

the class AppKeeper method initGlide.

/**
     * 图片加载框架Glide,使用OkHttp处理网络请求
     */
private static void initGlide(Application app) {
    OkHttpClient okHttpClient = BaseHttpRequest.getHttpClient();
    Glide.get(app).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}
Also used : OkHttpUrlLoader(com.bumptech.glide.integration.okhttp.OkHttpUrlLoader) OkHttpClient(com.squareup.okhttp.OkHttpClient)

Example 45 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project dropbox-sdk-java by dropbox.

the class OkHttpRequestor method defaultOkHttpClient.

/**
 * Returns an {@code OkHttpClient} instance with the default settings for this SDK.
 */
public static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    // enables certificate pinning
    client.setSslSocketFactory(SSLConfig.getSSLSocketFactory());
    return client;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient)

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