Search in sources :

Example 21 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 22 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 23 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 24 with Call

use of com.squareup.okhttp.Call in project ButterRemote-Android by se-bastiaan.

the class PopcornTimeRpcClient method request.

/**
 * Send JSON RPC request to the instance
 * @param rpc Request data
 * @param callback Callback for the request
 * @return ResponseFuture
 */
private Call request(final RpcRequest rpc, final Callback callback) {
    RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, mGson.toJson(rpc));
    Request request = new Request.Builder().url(mUrl).header("Authorization", Credentials.basic(mUsername, mPassword)).post(requestBody).build();
    Call call = mClient.newCall(request);
    call.enqueue(new com.squareup.okhttp.Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            callback.onCompleted(e, null);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            RpcResponse result = null;
            Exception e = null;
            try {
                if (response != null && response.isSuccessful()) {
                    String responseStr = response.body().string();
                    // LogUtils.d("PopcornTimeRpcClient", "Response: " + responseStr);
                    result = mGson.fromJson(responseStr, RpcResponse.class);
                    LinkedTreeMap<String, Object> map = result.getMapResult();
                    if (map.containsKey("popcornVersion")) {
                        mVersion = (String) map.get("popcornVersion");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                e = ex;
                mVersion = ZERO_VERSION;
                if (rpc.id == RequestId.GET_SELECTION.ordinal()) {
                    mVersion = "0.3.4";
                }
            }
            callback.onCompleted(e, result);
        }
    });
    return call;
}
Also used : Call(com.squareup.okhttp.Call) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Request(com.squareup.okhttp.Request) Callback(com.squareup.okhttp.Callback) IOException(java.io.IOException) IOException(java.io.IOException) Response(com.squareup.okhttp.Response) RequestBody(com.squareup.okhttp.RequestBody)

Example 25 with Call

use of com.squareup.okhttp.Call 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)

Aggregations

Call (com.squareup.okhttp.Call)46 Type (java.lang.reflect.Type)28 Request (com.squareup.okhttp.Request)13 Response (com.squareup.okhttp.Response)13 OkHttpClient (com.squareup.okhttp.OkHttpClient)9 PaymentDetails (jp.ne.paypay.model.PaymentDetails)8 RequestBody (com.squareup.okhttp.RequestBody)5 IOException (java.io.IOException)5 HttpUrl (com.squareup.okhttp.HttpUrl)4 HashMap (java.util.HashMap)4 ReverseCashbackDetails (jp.ne.paypay.model.ReverseCashbackDetails)4 TypeToken (com.google.gson.reflect.TypeToken)3 NotDataResponse (jp.ne.paypay.model.NotDataResponse)3 Base64 (org.apache.commons.codec.binary.Base64)3 Pair (jp.ne.paypay.Pair)2 Request (com.android.volley.Request)1 ApiCommand (com.cloudera.api.swagger.model.ApiCommand)1 ApiCommandList (com.cloudera.api.swagger.model.ApiCommandList)1 ApiHost (com.cloudera.api.swagger.model.ApiHost)1 ApiHostList (com.cloudera.api.swagger.model.ApiHostList)1