Search in sources :

Example 36 with RequestBody

use of com.squareup.okhttp.RequestBody in project xDrip by NightscoutFoundation.

the class GzipRequestInterceptor method sendFeedback.

public void sendFeedback(View myview) {
    final EditText contact = (EditText) findViewById(R.id.contactText);
    final EditText yourtext = (EditText) findViewById(R.id.yourText);
    final OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    client.interceptors().add(new GzipRequestInterceptor());
    if (yourtext.length() == 0) {
        toast("No text entered - cannot send blank");
        return;
    }
    if (contact.length() == 0) {
        toast("Without some contact info we cannot reply");
        askEmailAddress();
        return;
    }
    if (type_of_message.equals("Unknown")) {
        askType();
        return;
    }
    PersistentStore.setString(FEEDBACK_CONTACT_REFERENCE, contact.getText().toString());
    toast("Sending..");
    try {
        final RequestBody formBody = new FormEncodingBuilder().add("contact", contact.getText().toString()).add("body", JoH.getDeviceDetails() + "\n" + JoH.getVersionDetails() + "\n" + getBestCollectorHardwareName() + "\n===\n\n" + yourtext.getText().toString() + " \n\n===\nType: " + type_of_message + "\nLog data:\n\n" + log_data + "\n\n\nSent: " + JoH.dateTimeText(JoH.tsl())).add("rating", String.valueOf(myrating.getRating())).add("type", type_of_message).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 feedback request");
                    final Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        JoH.static_toast_long(response.body().string());
                        log_data = "";
                        // Home.toaststatic("Feedback sent successfully");
                        finish();
                    } else {
                        JoH.static_toast_short("Error sending feedback: " + response.message().toString());
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Got exception in execute: " + e.toString());
                    JoH.static_toast_short("Error with network connection");
                }
            }
        }).start();
    } catch (Exception e) {
        JoH.static_toast_short(e.getMessage());
        Log.e(TAG, "General exception: " + e.toString());
    }
}
Also used : EditText(android.widget.EditText) Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 37 with RequestBody

use of com.squareup.okhttp.RequestBody in project xDrip by NightscoutFoundation.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(com.squareup.okhttp.RequestBody)

Example 38 with RequestBody

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

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(com.squareup.okhttp.RequestBody)

Example 39 with RequestBody

use of com.squareup.okhttp.RequestBody 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) BaseAppCompatActivity(com.eveningoutpost.dexdrip.BaseAppCompatActivity) Activity(android.app.Activity) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

RequestBody (com.squareup.okhttp.RequestBody)39 Request (com.squareup.okhttp.Request)33 Response (com.squareup.okhttp.Response)24 IOException (java.io.IOException)19 OkHttpClient (com.squareup.okhttp.OkHttpClient)14 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)12 BufferedSink (okio.BufferedSink)7 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Call (com.squareup.okhttp.Call)3 Buffer (okio.Buffer)3 Activity (android.app.Activity)2 PowerManager (android.os.PowerManager)2 AppCompatActivity (android.support.v7.app.AppCompatActivity)2 EditText (android.widget.EditText)2 BaseAppCompatActivity (com.eveningoutpost.dexdrip.BaseAppCompatActivity)2 MediaType (com.squareup.okhttp.MediaType)2 CookieManager (java.net.CookieManager)2 GzipSink (okio.GzipSink)2 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)2