Search in sources :

Example 1 with Call

use of com.squareup.okhttp.Call in project yoo_home_Android by culturer.

the class OkHttpStack method performRequest.

@Override
public URLHttpResponse performRequest(Request<?> request, ArrayList<HttpParamsEntry> additionalHeaders) throws IOException {
    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());
    for (final HttpParamsEntry entry : request.getHeaders()) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    for (final HttpParamsEntry entry : additionalHeaders) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    setConnectionParametersForRequest(okHttpRequestBuilder, request);
    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    return responseFromConnection(okHttpResponse);
}
Also used : Response(com.squareup.okhttp.Response) URLHttpResponse(com.kymjs.rxvolley.http.URLHttpResponse) Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.kymjs.rxvolley.http.Request) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry)

Example 2 with Call

use of com.squareup.okhttp.Call in project fresco by facebook.

the class OkHttpNetworkFetcher method fetchWithRequest.

protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
    final Call call = mOkHttpClient.newCall(request);
    fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                call.cancel();
            } else {
                mCancellationExecutor.execute(new Runnable() {

                    @Override
                    public void run() {
                        call.cancel();
                    }
                });
            }
        }
    });
    call.enqueue(new com.squareup.okhttp.Callback() {

        @Override
        public void onResponse(Response response) {
            fetchState.responseTime = SystemClock.uptimeMillis();
            final ResponseBody body = response.body();
            try {
                if (!response.isSuccessful()) {
                    handleException(call, new IOException("Unexpected HTTP code " + response), callback);
                    return;
                }
                long contentLength = body.contentLength();
                if (contentLength < 0) {
                    contentLength = 0;
                }
                callback.onResponse(body.byteStream(), (int) contentLength);
            } catch (Exception e) {
                handleException(call, e, callback);
            } finally {
                try {
                    body.close();
                } catch (Exception e) {
                    FLog.w(TAG, "Exception when closing response body", e);
                }
            }
        }

        @Override
        public void onFailure(final Request request, final IOException e) {
            handleException(call, e, callback);
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Call(com.squareup.okhttp.Call) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 3 with Call

use of com.squareup.okhttp.Call in project PocketHub by pockethub.

the class ImageBinPoster method post.

/**
     * Post the image to ImageBin
     *
     * @param bytes Bytes of the image to post
     * @param callback Request callback
     */
public static void post(byte[] bytes, Callback callback) {
    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("file", "test", RequestBody.create(MediaType.parse("image/*"), bytes)).build();
    Request request = new Request.Builder().url("https://imagebin.ca/upload.php").post(requestBody).build();
    OkHttpClient client = new OkHttpClient();
    Call call = client.newCall(request);
    call.enqueue(callback);
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) RequestBody(com.squareup.okhttp.RequestBody)

Example 4 with Call

use of com.squareup.okhttp.Call in project hale by halestudio.

the class ProjectStoreHelper method executePlainTextCallWithFeedback.

/**
 * Execute a REST call with the content type <code>text/plain; charset=utf-8
 * </code>
 *
 * @param method HTTP method (POST, PUT)
 * @param path REST path (without base path)
 * @param body The plain text bdoy
 * @param basePath The REST base path
 * @param apiKey The API key
 * @return the feedback
 * @throws HaleConnectException thrown on any API error
 */
public static Feedback executePlainTextCallWithFeedback(String method, String path, String body, BasePathResolver basePath, String apiKey) throws HaleConnectException {
    ApiClient apiClient = ProjectStoreHelper.getApiClient(basePath, apiKey);
    OkHttpClient httpClient = apiClient.getHttpClient();
    String url = apiClient.buildUrl(path, null);
    Request.Builder reqBuilder = new Request.Builder().url(url);
    Map<String, String> headerParams = new HashMap<String, String>();
    apiClient.updateParamsForAuth(new String[] { "bearer" }, null, headerParams);
    apiClient.processHeaderParams(headerParams, reqBuilder);
    RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), body);
    Request request = reqBuilder.method(method, reqBody).build();
    Call call = httpClient.newCall(request);
    Feedback feedback;
    try {
        ApiResponse<Feedback> resp = apiClient.execute(call, new TypeToken<Feedback>() {
        }.getType());
        feedback = resp.getData();
    } catch (com.haleconnect.api.projectstore.v1.ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    return feedback;
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) HashMap(java.util.HashMap) Request(com.squareup.okhttp.Request) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) ApiClient(com.haleconnect.api.projectstore.v1.ApiClient) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) TypeToken(com.google.gson.reflect.TypeToken) RequestBody(com.squareup.okhttp.RequestBody)

Example 5 with Call

use of com.squareup.okhttp.Call in project java by kubernetes-client.

the class PodLogs method streamNamespacedPodLog.

// Important note. You must close this stream or else you can leak connections.
public InputStream streamNamespacedPodLog(String namespace, String name, String container, Integer sinceSeconds, Integer tailLines, boolean timestamps) throws ApiException, IOException {
    Call call = coreClient.readNamespacedPodLogCall(name, namespace, container, true, null, "false", false, sinceSeconds, tailLines, timestamps, null, null);
    Response response = call.execute();
    if (!response.isSuccessful()) {
        throw new ApiException("Logs request failed: " + response.code());
    }
    return response.body().byteStream();
}
Also used : Response(com.squareup.okhttp.Response) Call(com.squareup.okhttp.Call)

Aggregations

Call (com.squareup.okhttp.Call)7 Response (com.squareup.okhttp.Response)5 OkHttpClient (com.squareup.okhttp.OkHttpClient)4 Request (com.squareup.okhttp.Request)4 RequestBody (com.squareup.okhttp.RequestBody)3 IOException (java.io.IOException)2 Request (com.android.volley.Request)1 BaseProducerContextCallbacks (com.facebook.imagepipeline.producers.BaseProducerContextCallbacks)1 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 TypeToken (com.google.gson.reflect.TypeToken)1 ApiClient (com.haleconnect.api.projectstore.v1.ApiClient)1 Feedback (com.haleconnect.api.projectstore.v1.model.Feedback)1 Request (com.kymjs.rxvolley.http.Request)1 URLHttpResponse (com.kymjs.rxvolley.http.URLHttpResponse)1 HttpParamsEntry (com.kymjs.rxvolley.toolbox.HttpParamsEntry)1 Callback (com.squareup.okhttp.Callback)1 Headers (com.squareup.okhttp.Headers)1 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)1