Search in sources :

Example 81 with Response

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

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 82 with Response

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

the class DisplayQRCode method uploadBytes.

public static void uploadBytes(byte[] result, final int callback_option) {
    final PowerManager.WakeLock wl = JoH.getWakeLock("uploadBytes", 1200000);
    if ((result != null) && (result.length > 0)) {
        final byte[] mykey = CipherUtils.getRandomKey();
        byte[] crypted_data = CipherUtils.encryptBytes(JoH.compressBytesToBytes(result), mykey);
        if ((crypted_data != null) && (crypted_data.length > 0)) {
            Log.d(TAG, "Before: " + result.length + " After: " + crypted_data.length);
            final OkHttpClient client = new OkHttpClient();
            client.setConnectTimeout(15, TimeUnit.SECONDS);
            client.setReadTimeout(30, TimeUnit.SECONDS);
            client.setWriteTimeout(30, TimeUnit.SECONDS);
            toast("Preparing");
            try {
                send_url = xdrip.getAppContext().getString(R.string.wserviceurl) + "/joh-setsw";
                final String bbody = Base64.encodeToString(crypted_data, Base64.NO_WRAP);
                Log.d(TAG, "Upload Body size: " + bbody.length());
                final RequestBody formBody = new FormEncodingBuilder().add("data", bbody).build();
                new Thread(new Runnable() {

                    public void run() {
                        try {
                            final Request request = new Request.Builder().header("User-Agent", "Mozilla/5.0 (jamorham)").header("Connection", "close").url(send_url).post(formBody).build();
                            Log.i(TAG, "Uploading data");
                            Response response = client.newCall(request).execute();
                            if (response.isSuccessful()) {
                                final String reply = response.body().string();
                                Log.d(TAG, "Got success response length: " + reply.length() + " " + reply);
                                if ((reply.length() == 35) && (reply.startsWith("ID:"))) {
                                    switch(callback_option) {
                                        case 1:
                                            {
                                                if (mInstance != null) {
                                                    mInstance.display_final_all_settings_qr_code(reply.substring(3, 35), mykey);
                                                } else {
                                                    Log.e(TAG, "mInstance null");
                                                }
                                                break;
                                            }
                                        case 2:
                                            {
                                                GcmActivity.backfillLink(reply.substring(3, 35), JoH.bytesToHex(mykey));
                                                break;
                                            }
                                        default:
                                            {
                                                toast("Invalid callback option on upload");
                                            }
                                    }
                                } else {
                                    Log.d(TAG, "Got unhandled reply: " + reply);
                                    toast(reply);
                                }
                            } else {
                                toast("Error please try again");
                            }
                        } catch (Exception e) {
                            Log.e(TAG, "Got exception in execute: " + e.toString());
                            e.printStackTrace();
                            toast("Error with connection");
                        }
                    }
                }).start();
            } catch (Exception e) {
                toast(e.getMessage());
                Log.e(TAG, "General exception: " + e.toString());
            } finally {
                JoH.releaseWakeLock(wl);
            }
        } else {
            toast("Something went wrong preparing the settings");
        }
    } else {
        toast("Could not read data somewhere");
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) PowerManager(android.os.PowerManager) Response(com.squareup.okhttp.Response) RequestBody(com.squareup.okhttp.RequestBody)

Example 83 with Response

use of com.squareup.okhttp.Response in project remusic by aa112901.

the class HttpUtil method getResposeJsonObject.

public static JsonObject getResposeJsonObject(String action1, Context context, boolean forceCache) {
    try {
        Log.e("action-cache", action1);
        File sdcache = context.getCacheDir();
        // File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
        // 30Mb
        Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
        mOkHttpClient.setCache(cache);
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request.Builder builder = new Request.Builder().url(action1);
        if (forceCache) {
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        Request request = builder.build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String c = response.body().string();
            Log.e("cache", c);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(c);
            return el.getAsJsonObject();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) JsonElement(com.google.gson.JsonElement) Request(com.squareup.okhttp.Request) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cache(com.squareup.okhttp.Cache) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) JsonParser(com.google.gson.JsonParser)

Example 84 with Response

use of com.squareup.okhttp.Response in project remusic by aa112901.

the class HttpUtil method getBitmapStream.

public static Bitmap getBitmapStream(Context context, String url, boolean forceCache) {
    try {
        File sdcache = context.getExternalCacheDir();
        // File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
        // 30Mb
        Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
        mOkHttpClient.setCache(cache);
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request.Builder builder = new Request.Builder().url(url);
        if (forceCache) {
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        Request request = builder.build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            return _decodeBitmapFromStream(response.body().byteStream(), 160, 160);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cache(com.squareup.okhttp.Cache) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache)

Example 85 with Response

use of com.squareup.okhttp.Response in project spring-boot-quick by vector4wang.

the class UseHeaders method main.

public static void main(String[] args) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url("http://www.baidu.com").header("User-Agent", "My super agent").addHeader("Accept", "text/html").build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new IOException("服务器端错误: " + response);
    }
    System.out.println(response.header("Server"));
    System.out.println(response.headers("Set-Cookie"));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

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