Search in sources :

Example 66 with Call

use of zipkin2.Call in project BaseProject by feer921.

the class RetrofitClient method cancelAllCall.

/**
 * 取消所以添加过的网络请求
 */
public void cancelAllCall() {
    if (cachedCalls == null || cachedCalls.isEmpty()) {
        return;
    }
    synchronized (syncLockObj) {
        Iterator<Call> callIterator = cachedCalls.keySet().iterator();
        while (callIterator.hasNext()) {
            Call curCall = callIterator.next();
            if (curCall != null) {
                curCall.cancel();
            }
        }
        cachedCalls.clear();
    }
}
Also used : Call(retrofit2.Call)

Example 67 with Call

use of zipkin2.Call in project libsignal-service-java by signalapp.

the class PushServiceSocket method getServiceConnection.

private Response getServiceConnection(String urlFragment, String method, String body) throws PushNetworkException {
    try {
        ConnectionHolder connectionHolder = getRandom(serviceClients, random);
        OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
        Log.w(TAG, "Push service URL: " + connectionHolder.getUrl());
        Log.w(TAG, "Opening URL: " + String.format("%s%s", connectionHolder.getUrl(), urlFragment));
        Request.Builder request = new Request.Builder();
        request.url(String.format("%s%s", connectionHolder.getUrl(), urlFragment));
        if (body != null) {
            request.method(method, RequestBody.create(MediaType.parse("application/json"), body));
        } else {
            request.method(method, null);
        }
        if (credentialsProvider.getPassword() != null) {
            request.addHeader("Authorization", getAuthorizationHeader(credentialsProvider));
        }
        if (userAgent != null) {
            request.addHeader("X-Signal-Agent", userAgent);
        }
        if (connectionHolder.getHostHeader().isPresent()) {
            request.addHeader("Host", connectionHolder.getHostHeader().get());
        }
        Call call = okHttpClient.newCall(request.build());
        synchronized (connections) {
            connections.add(call);
        }
        try {
            return call.execute();
        } finally {
            synchronized (connections) {
                connections.remove(call);
            }
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) Request(okhttp3.Request) IOException(java.io.IOException)

Example 68 with Call

use of zipkin2.Call in project libsignal-service-java by signalapp.

the class PushServiceSocket method uploadToCdn.

private byte[] uploadToCdn(String acl, String key, String policy, String algorithm, String credential, String date, String signature, InputStream data, String contentType, long length, OutputStreamFactory outputStreamFactory) throws PushNetworkException, NonSuccessfulResponseCodeException {
    ConnectionHolder connectionHolder = getRandom(cdnClients, random);
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length);
    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("acl", acl).addFormDataPart("key", key).addFormDataPart("policy", policy).addFormDataPart("Content-Type", contentType).addFormDataPart("x-amz-algorithm", algorithm).addFormDataPart("x-amz-credential", credential).addFormDataPart("x-amz-date", date).addFormDataPart("x-amz-signature", signature).addFormDataPart("file", "file", file).build();
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl()).post(requestBody);
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    try {
        Response response;
        try {
            response = call.execute();
        } catch (IOException e) {
            throw new PushNetworkException(e);
        }
        if (response.isSuccessful())
            return file.getTransmittedDigest();
        else
            throw new NonSuccessfulResponseCodeException("Response: " + response);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) DigestingRequestBody(org.whispersystems.signalservice.internal.push.http.DigestingRequestBody) Request(okhttp3.Request) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) Response(okhttp3.Response) MultipartBody(okhttp3.MultipartBody) DigestingRequestBody(org.whispersystems.signalservice.internal.push.http.DigestingRequestBody) RequestBody(okhttp3.RequestBody)

Example 69 with Call

use of zipkin2.Call in project openhab-android by openhab.

the class OpenHABNotificationFragment method loadNotifications.

private void loadNotifications() {
    Connection conn = ConnectionFactory.getConnection(Connection.TYPE_CLOUD);
    if (conn == null) {
        return;
    }
    startProgressIndicator();
    mRequestHandle = conn.getAsyncHttpClient().get("/api/v1/notifications?limit=20", new MyHttpClient.ResponseHandler() {

        @Override
        public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
            stopProgressIndicator();
            Log.d(TAG, "Notifications request success");
            try {
                String jsonString = new String(responseBody, "UTF-8");
                JSONArray jsonArray = new JSONArray(jsonString);
                Log.d(TAG, jsonArray.toString());
                mNotifications.clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    try {
                        JSONObject sitemapJson = jsonArray.getJSONObject(i);
                        mNotifications.add(OpenHABNotification.fromJson(sitemapJson));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                mNotificationAdapter.notifyDataSetChanged();
            } catch (UnsupportedEncodingException | JSONException e) {
                Log.d(TAG, e.getMessage(), e);
            }
        }

        @Override
        public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
            stopProgressIndicator();
            Log.e(TAG, "Notifications request failure");
        }
    });
}
Also used : Call(okhttp3.Call) JSONObject(org.json.JSONObject) Headers(okhttp3.Headers) Connection(org.openhab.habdroid.core.connection.Connection) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 70 with Call

use of zipkin2.Call in project openhab-android by openhab.

the class MySyncHttpClient method method.

protected Response method(String url, String method, Map<String, String> addHeaders, String requestBody, String mediaType, final ResponseHandler responseHandler) {
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(getBaseUrl().newBuilder(url).build());
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        requestBuilder.addHeader(entry.getKey(), entry.getValue());
    }
    if (addHeaders != null) {
        for (Map.Entry<String, String> entry : addHeaders.entrySet()) {
            requestBuilder.addHeader(entry.getKey(), entry.getValue());
        }
    }
    if (requestBody != null) {
        requestBuilder.method(method, RequestBody.create(MediaType.parse(mediaType), requestBody));
    }
    Request request = requestBuilder.build();
    Call call = client.newCall(request);
    try {
        Response resp = call.execute();
        if (resp.isSuccessful()) {
            responseHandler.onSuccess(call, resp.code(), resp.headers(), resp.body().bytes());
        } else {
            responseHandler.onFailure(call, resp.code(), resp.headers(), resp.body().bytes(), new IOException(resp.code() + ": " + resp.message()));
        }
        return resp;
    } catch (IOException ex) {
        responseHandler.onFailure(call, 0, new Headers.Builder().build(), null, ex);
        return new Response.Builder().code(500).message(ex.getClass().getName() + ": " + ex.getMessage()).request(request).protocol(Protocol.HTTP_1_0).build();
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) Map(java.util.Map)

Aggregations

Call (okhttp3.Call)409 Response (okhttp3.Response)309 Request (okhttp3.Request)282 IOException (java.io.IOException)232 Call (retrofit2.Call)134 Callback (okhttp3.Callback)133 OkHttpClient (okhttp3.OkHttpClient)98 Test (org.junit.Test)88 ResponseBody (okhttp3.ResponseBody)76 RequestBody (okhttp3.RequestBody)58 Retrofit (retrofit2.Retrofit)48 Gson (com.google.gson.Gson)47 Response (retrofit2.Response)47 File (java.io.File)44 Headers (okhttp3.Headers)41 Callback (retrofit2.Callback)41 GsonBuilder (com.google.gson.GsonBuilder)40 JSONObject (org.json.JSONObject)39 MockResponse (okhttp3.mockwebserver.MockResponse)38 List (java.util.List)35