use of com.squareup.okhttp.OkHttpClient 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.OkHttpClient in project Rutgers-Course-Tracker by tevjef.
the class ClientModule method providesRMPClient.
@Provides
@Singleton
public RMPClient providesRMPClient(OkHttpClient client, Gson gson) {
OkHttpClient okClient = client.clone();
okClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
okClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://rutgersapp.tevindev.me:8080/").setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new GsonConverter(gson)).setClient(new OkClient(okClient)).build();
return new RMPClient(restAdapter.create(ClientService.class));
}
use of com.squareup.okhttp.OkHttpClient in project Rutgers-Course-Tracker by tevjef.
the class ScraperModule method providesRMP.
@Provides
@Singleton
public RMPScraper providesRMP(OkHttpClient client) {
OkHttpClient okClient = client.clone();
okClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
okClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
//okClient.networkInterceptors().add(getCacheControlInterceptor(TimeUnit.DAYS.toMillis(7)));
return new RMPScraper(okClient);
}
use of com.squareup.okhttp.OkHttpClient in project Rutgers-Course-Tracker by tevjef.
the class RutgersCTModule method providesOkHttpClient.
@Provides
@Singleton
public OkHttpClient providesOkHttpClient(Context context) {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
client.networkInterceptors().add(new StethoInterceptor());
File httpCacheDir = new File(context.getCacheDir(), context.getString(R.string.application_name));
// 50 MiB
long httpCacheSize = 50 * 1024 * 1024;
Cache cache = new Cache(httpCacheDir, httpCacheSize);
client.setCache(cache);
if (BuildConfig.DEBUG) {
try {
cache.evictAll();
} catch (IOException e) {
e.printStackTrace();
}
}
return client;
}
use of com.squareup.okhttp.OkHttpClient in project Rutgers-Course-Tracker by tevjef.
the class RetroRutgersModule method providesRutgersRestAdapter.
@Provides
@Singleton
public RetroRutgersService providesRutgersRestAdapter(OkHttpClient client, Gson gson) {
OkHttpClient okClient = client.clone();
okClient.networkInterceptors().add(getCacheControlInterceptor(TimeUnit.SECONDS.toMillis(5)));
return new RestAdapter.Builder().setEndpoint("http://sis.rutgers.edu/soc/").setLogLevel(RestAdapter.LogLevel.HEADERS_AND_ARGS).setErrorHandler(new MyErrorHandler()).setClient(new OkClient(okClient)).setConverter(new GsonConverter(gson)).build().create(RetroRutgersService.class);
}
Aggregations