Search in sources :

Example 1 with RequestBody

use of com.squareup.okhttp.RequestBody in project hadoop by apache.

the class ConfRefreshTokenBasedAccessTokenProvider method refresh.

void refresh() throws IOException {
    try {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        String bodyString = Utils.postBody(GRANT_TYPE, REFRESH_TOKEN, REFRESH_TOKEN, refreshToken, CLIENT_ID, clientId);
        RequestBody body = RequestBody.create(URLENCODED, bodyString);
        Request request = new Request.Builder().url(refreshURL).post(body).build();
        Response responseBody = client.newCall(request).execute();
        if (responseBody.code() != HttpStatus.SC_OK) {
            throw new IllegalArgumentException("Received invalid http response: " + responseBody.code() + ", text = " + responseBody.toString());
        }
        Map<?, ?> response = READER.readValue(responseBody.body().string());
        String newExpiresIn = response.get(EXPIRES_IN).toString();
        accessTokenTimer.setExpiresIn(newExpiresIn);
        accessToken = response.get(ACCESS_TOKEN).toString();
    } catch (Exception e) {
        throw new IOException("Exception while refreshing access token", e);
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 2 with RequestBody

use of com.squareup.okhttp.RequestBody in project hadoop by apache.

the class CredentialBasedAccessTokenProvider method refresh.

void refresh() throws IOException {
    try {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(), GRANT_TYPE, CLIENT_CREDENTIALS, CLIENT_ID, clientId);
        RequestBody body = RequestBody.create(URLENCODED, bodyString);
        Request request = new Request.Builder().url(refreshURL).post(body).build();
        Response responseBody = client.newCall(request).execute();
        if (responseBody.code() != HttpStatus.SC_OK) {
            throw new IllegalArgumentException("Received invalid http response: " + responseBody.code() + ", text = " + responseBody.toString());
        }
        Map<?, ?> response = READER.readValue(responseBody.body().string());
        String newExpiresIn = response.get(EXPIRES_IN).toString();
        timer.setExpiresIn(newExpiresIn);
        accessToken = response.get(ACCESS_TOKEN).toString();
    } catch (Exception e) {
        throw new IOException("Unable to obtain access token from credential", e);
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 3 with RequestBody

use of com.squareup.okhttp.RequestBody in project openzaly by akaxincom.

the class HttpTestPost method post.

static String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(body).build();
    Response response = client.newCall(request).execute();
    System.out.println("response = " + response.isSuccessful());
    if (response.isSuccessful()) {
        return response.body().string();
    } else {
        throw new IOException("Unexpected code " + response);
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 4 with RequestBody

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

the class GzipRequestInterceptor method forceContentLength.

/**
 * https://github.com/square/okhttp/issues/350
 */
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return new RequestBody() {

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

        @Override
        public long contentLength() {
            return buffer.size();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
        }
    };
}
Also used : Buffer(okio.Buffer) BufferedSink(okio.BufferedSink) RequestBody(com.squareup.okhttp.RequestBody)

Example 5 with RequestBody

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

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)

Aggregations

RequestBody (com.squareup.okhttp.RequestBody)51 Request (com.squareup.okhttp.Request)33 Response (com.squareup.okhttp.Response)24 IOException (java.io.IOException)23 OkHttpClient (com.squareup.okhttp.OkHttpClient)14 ResponseBody (com.squareup.okhttp.ResponseBody)13 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)12 JSONArray (org.json.JSONArray)12 JSONObject (org.json.JSONObject)10 BufferedSink (okio.BufferedSink)7 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URISyntaxException (java.net.URISyntaxException)4 JSONException (org.json.JSONException)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