Search in sources :

Example 61 with Response

use of com.squareup.okhttp.Response in project cw-omnibus by commonsguy.

the class BackupService method uploadBackup.

private void uploadBackup(File backup) throws IOException {
    Request request = new Request.Builder().url(URL_CREATE_BACKUP).post(RequestBody.create(JSON, "{}")).build();
    Response response = OKHTTP_CLIENT.newCall(request).execute();
    if (response.code() == 201) {
        String backupURL = response.header("Location");
        request = new Request.Builder().url(backupURL + RESOURCE_DATASET).put(RequestBody.create(ZIP, backup)).build();
        response = OKHTTP_CLIENT.newCall(request).execute();
        if (response.code() == 201) {
            String datasetURL = response.header("Location");
            SharedPreferences prefs = getSharedPreferences(getClass().getName(), Context.MODE_PRIVATE);
            prefs.edit().putString(PREF_LAST_BACKUP_DATASET, datasetURL).commit();
        } else {
            Log.e(getClass().getSimpleName(), "Unsuccessful request to upload backup");
        }
    } else {
        Log.e(getClass().getSimpleName(), "Unsuccessful request to create backup");
    }
}
Also used : Response(com.squareup.okhttp.Response) SharedPreferences(android.content.SharedPreferences) Request(com.squareup.okhttp.Request)

Example 62 with Response

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

use of com.squareup.okhttp.Response in project Notes by lguipeng.

the class TAndroidTransport method flush.

@Override
public void flush() throws TTransportException {
    Util.closeQuietly(mResponseBody);
    mResponseBody = null;
    RequestBody requestBody = new RequestBody() {

        @Override
        public MediaType contentType() {
            if (mHeaders != null && mHeaders.containsKey("Content-Type")) {
                return MediaType.parse(mHeaders.get("Content-Type"));
            } else {
                return MEDIA_TYPE_THRIFT;
            }
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            copy(mByteStore.getInputStream(), sink.outputStream());
        }
    };
    try {
        Request.Builder builder = new Request.Builder().url(mUrl).post(requestBody);
        if (mHeaders != null) {
            for (String name : mHeaders.keySet()) {
                builder.header(name, mHeaders.get(name));
            }
        }
        Response response = mHttpClient.newCall(builder.build()).execute();
        if (response.code() != 200) {
            throw new TTransportException("HTTP Response code: " + response.code() + ", message " + response.message());
        }
        mResponseBody = response.body().byteStream();
    } catch (Exception e) {
        throw new TTransportException(e);
    } finally {
        try {
            mByteStore.reset();
        } catch (IOException ignored) {
        }
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) TTransportException(com.evernote.thrift.transport.TTransportException) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException) RequestBody(com.squareup.okhttp.RequestBody)

Example 64 with Response

use of com.squareup.okhttp.Response in project actor-platform by actorapp.

the class ActorPushRegister method registerForPush.

public static void registerForPush(final Context context, String endpoint, final Callback callback) {
    Runtime.dispatch(() -> {
        final SharedPreferences sharedPreferences = context.getSharedPreferences("actor_push_register", Context.MODE_PRIVATE);
        String registrationEndpoint = sharedPreferences.getString("registration_endpoint", null);
        String registrationData = sharedPreferences.getString("registration_data", null);
        OkHttpClient client = new OkHttpClient();
        if (registrationEndpoint != null && registrationData != null) {
            try {
                JSONObject data = new JSONObject(registrationData);
                startService(data, context);
                callback.onRegistered(registrationEndpoint);
                return;
            } catch (JSONException e) {
                e.printStackTrace();
                sharedPreferences.edit().clear().commit();
            }
        }
        final Request request = new Request.Builder().url(endpoint).method("POST", RequestBody.create(MediaType.parse("application/json"), "{}")).build();
        client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Log.d("ACTOR_PUSH", "ACTOR_PUSH not registered: " + e.getMessage());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String res = response.body().string();
                    JSONObject js = new JSONObject(res).getJSONObject("data");
                    String endpoint1 = js.getString("endpoint");
                    sharedPreferences.edit().putString("registration_endpoint", endpoint1).putString("registration_data", js.toString()).commit();
                    startService(js, context);
                    Log.d("ActorPushRegister", "Endpoint: " + endpoint1);
                    callback.onRegistered(endpoint1);
                } catch (JSONException e) {
                    e.printStackTrace();
                // TODO: Handle?
                }
            }
        });
    });
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) SharedPreferences(android.content.SharedPreferences) Request(com.squareup.okhttp.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject)

Example 65 with Response

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

the class HttpUtil method postUrl.

public static void postUrl(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().build();
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(formBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

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