Search in sources :

Example 91 with Response

use of com.squareup.okhttp.Response in project xDrip-plus by jamorham.

the class WebAppHelper method doInBackground.

@Override
protected Integer doInBackground(String... url) {
    try {
        Log.d(TAG, "Processing URL: " + url[0]);
        Request request = new Request.Builder().header("User-Agent", "Mozilla/5.0 (jamorham)").header("Connection", "close").url(url[0]).build();
        client.setConnectTimeout(15, TimeUnit.SECONDS);
        client.setReadTimeout(30, TimeUnit.SECONDS);
        client.setWriteTimeout(30, TimeUnit.SECONDS);
        final Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);
        body = response.body().bytes();
    } catch (Exception e) {
        Log.d(TAG, "Exception in background task: " + e.toString());
    }
    return body.length;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException)

Example 92 with Response

use of com.squareup.okhttp.Response in project xDrip-plus by jamorham.

the class LanguageEditor method uploadData.

private void uploadData(String data) {
    if (data.length() < 10) {
        // don't translate
        toast("No text entered - cannot send blank");
        return;
    }
    final String email = LanguageStore.getString(EMAIL_KEY);
    final String name = LanguageStore.getString(NAME_KEY);
    final String consent = LanguageStore.getString(CONSENT_KEY);
    if (email.length() == 0) {
        // don't translate
        toast("No email address stored - cannot send");
        return;
    }
    if (name.length() == 0) {
        // don't translate
        toast("No thanks name stored - cannot send");
        return;
    }
    if (consent.length() == 0) {
        // don't translate
        toast("No consent stored - cannot send");
        return;
    }
    final OkHttpClient client = new OkHttpClient();
    final String send_url = mContext.getString(R.string.wserviceurl) + "/joh-langdata";
    client.setConnectTimeout(20, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    // don't translate
    toast("Sending..");
    try {
        final RequestBody formBody = new FormEncodingBuilder().add("locale", Locale.getDefault().toString()).add("contact", email).add("name", name).add("consent", consent).add("data", data).build();
        new Thread(new Runnable() {

            public void run() {
                try {
                    final Request request = new Request.Builder().url(send_url).post(formBody).build();
                    Log.i(TAG, "Sending language data");
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        toast("data sent successfully");
                        ((Activity) mContext).runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    saveBtn.setVisibility(View.INVISIBLE);
                                    undoBtn.setVisibility(View.INVISIBLE);
                                } catch (Exception e) {
                                // do nothing if its gone away
                                }
                            }
                        });
                    } else {
                        toast("Error sending data: " + response.message());
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Got exception in execute: " + e.toString());
                    // don't translate
                    toast("Error with network connection");
                }
            }
        }).start();
    } catch (Exception e) {
        toast(e.getMessage());
        Log.e(TAG, "General exception: " + e.toString());
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) AppCompatActivity(android.support.v7.app.AppCompatActivity) Activity(android.app.Activity) RequestBody(com.squareup.okhttp.RequestBody)

Example 93 with Response

use of com.squareup.okhttp.Response 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 94 with Response

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

the class PushClient method pushData.

/**
 * Pushes data to DHIS Server
 */
private JSONObject pushData(JSONObject data) throws ApiCallException {
    Response response = null;
    final String DHIS_URL = getDhisURL();
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    // connect timeout
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    // socket timeout
    client.setReadTimeout(30, TimeUnit.SECONDS);
    // write timeout
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    // Cancel retry on failure
    client.setRetryOnConnectionFailure(false);
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    RequestBody body = RequestBody.create(JSON, data.toString());
    Request request = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL).post(body).build();
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new ApiCallException(e);
    }
    return ServerApiUtils.getApiResponseAsJSONObject(response);
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 95 with Response

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

the class ServerAPIController method getServerVersion.

/**
 * Returns the version of the given server.
 * Null if something went wrong
 */
public static String getServerVersion(String url) throws ApiCallException {
    String serverVersion = null;
    String urlServerInfo = url + DHIS_SERVER_INFO;
    Response response = ServerApiCallExecution.executeCall(null, urlServerInfo, "GET");
    JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
    if (body.has(TAG_VERSION)) {
        try {
            serverVersion = body.getString(TAG_VERSION);
        } catch (JSONException e) {
            new ApiCallException(e);
        }
    }
    if (serverVersion != null) {
        Log.i(TAG, String.format("getServerVersion(%s) -> %s", url, serverVersion));
    }
    return serverVersion;
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Aggregations

Response (com.squareup.okhttp.Response)109 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)70 OkHttpClient (com.squareup.okhttp.OkHttpClient)31 RequestBody (com.squareup.okhttp.RequestBody)23 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)10 Callback (com.squareup.okhttp.Callback)9 SocketTimeoutException (java.net.SocketTimeoutException)8 File (java.io.File)7 ShowException (org.eyeseetea.malariacare.views.ShowException)7 Buffer (okio.Buffer)6 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 Call (com.squareup.okhttp.Call)4 ApiException (io.kubernetes.client.ApiException)4