Search in sources :

Example 31 with OkHttpClient

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?
                }
            }
        });
    });
}
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 32 with OkHttpClient

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));
}
Also used : GsonConverter(retrofit.converter.GsonConverter) RMPClient(com.tevinjeffrey.rmp.client.RMPClient) OkHttpClient(com.squareup.okhttp.OkHttpClient) ClientService(com.tevinjeffrey.rmp.client.ClientService) OkClient(retrofit.client.OkClient) RestAdapter(retrofit.RestAdapter) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 33 with OkHttpClient

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);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) RMPScraper(com.tevinjeffrey.rmp.scraper.RMPScraper) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 34 with OkHttpClient

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;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp.StethoInterceptor) IOException(java.io.IOException) File(java.io.File) Cache(com.squareup.okhttp.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 35 with OkHttpClient

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);
}
Also used : GsonConverter(retrofit.converter.GsonConverter) OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RestAdapter(retrofit.RestAdapter) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2