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;
}
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;
}
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?
}
}
});
});
}
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()));
}
}
});
});
}
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);
}
Aggregations