Search in sources :

Example 51 with Request

use of com.squareup.okhttp.Request 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 52 with Request

use of com.squareup.okhttp.Request 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 53 with Request

use of com.squareup.okhttp.Request 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 54 with Request

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

the class AndroidHttpProvider method getMethod.

@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
    return new Promise<>(resolver -> {
        final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
        Log.d(TAG, "Downloading part: " + request.toString());
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Log.d(TAG, "Downloading part error: " + request.toString());
                e.printStackTrace();
                resolver.error(new HTTPError(0));
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
                if (response.code() >= 200 && response.code() < 300) {
                    resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
                } else {
                    resolver.error(new HTTPError(response.code()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 55 with Request

use of com.squareup.okhttp.Request in project SimpleNews by liuling07.

the class OkHttpUtils method postRequest.

private void postRequest(String url, final ResultCallback callback, List<Param> params) {
    Request request = buildPostRequest(url, params);
    deliveryResult(callback, request);
}
Also used : Request(com.squareup.okhttp.Request)

Aggregations

Request (com.squareup.okhttp.Request)74 Response (com.squareup.okhttp.Response)47 IOException (java.io.IOException)40 OkHttpClient (com.squareup.okhttp.OkHttpClient)22 RequestBody (com.squareup.okhttp.RequestBody)18 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 Callback (com.squareup.okhttp.Callback)9 File (java.io.File)9 InputStream (java.io.InputStream)6 Gson (com.google.gson.Gson)4 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 MediaType (com.squareup.okhttp.MediaType)4 ResponseBody (com.squareup.okhttp.ResponseBody)4 FileOutputStream (java.io.FileOutputStream)4 Test (org.junit.Test)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Uri (android.net.Uri)3 Cache (com.squareup.okhttp.Cache)3