Example usage for com.squareup.okhttp OkHttpClient OkHttpClient

List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient OkHttpClient.

Prototype

public OkHttpClient() 

Source Link

Usage

From source file:com.auth0.android.lock.LockActivity.java

License:Open Source License

@SuppressWarnings("unused")
@Subscribe/*from w  w  w.  j a  v a2s  .  co  m*/
public void onFetchApplicationRequest(FetchApplicationEvent event) {
    if (applicationFetcher == null) {
        applicationFetcher = new ApplicationFetcher(options.getAccount(), new OkHttpClient());
        applicationFetcher.fetch(applicationCallback);
    }
}

From source file:com.auth0.android.management.UsersAPIClient.java

License:Open Source License

/**
 * Creates a new API client instance providing Auth0 account info.
 *
 * @param auth0 account information/*from   w w w. j a  v a  2 s . com*/
 * @param token of the primary identity
 */
public UsersAPIClient(Auth0 auth0, String token) {
    this(auth0, new RequestFactory(token), new OkHttpClient(), GsonProvider.buildGson());
}

From source file:com.auth0.authentication.AuthenticationAPIClient.java

License:Open Source License

/**
 * Creates a new API client instance providing Auth0 account info.
 *
 * @param auth0 account information//from www. j  av  a 2 s .  co  m
 */
public AuthenticationAPIClient(Auth0 auth0) {
    this(auth0, new OkHttpClient(), new ObjectMapper());
}

From source file:com.av.remusic.lastfmapi.RestServiceFactory.java

License:Open Source License

public static <T> T create(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();

    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);

    RequestInterceptor interceptor = new RequestInterceptor() {
        @Override//from www.  j  a va 2s.c  om
        public void intercept(RequestFacade request) {
            //7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d",
                    Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };

    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl)
            .setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));

    return builder.build().create(clazz);

}

From source file:com.ayuget.redface.network.HTTPClientProvider.java

License:Apache License

private void initHttpClient() {
    httpClient = new OkHttpClient();

    if (settings.isProxyEnabled()) {
        enableHTTPProxy();/*from  w  w  w. ja v a  2 s.c  o m*/
    }

    httpClient.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT));

    httpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    httpClient.setReadTimeout(10, TimeUnit.SECONDS);
    httpClient.setWriteTimeout(10, TimeUnit.SECONDS);
}

From source file:com.ayuget.redface.network.HTTPRedirection.java

License:Apache License

/**
 * Resolves a redirected URL {@code originalUrl} to its final location.
 *
 * If the URL is not really redirected, the original URL is returned.
 *//* w w  w  .  j  a va 2 s  .  co m*/
public static Observable<String> resolve(final String originalUrl) {
    return Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            OkHttpClient httpClient = new OkHttpClient();
            httpClient.setFollowRedirects(false);

            Request request = new Request.Builder().url(originalUrl).build();

            try {
                Response response = httpClient.newCall(request).execute();

                if (response.code() == REDIRECTED_STATUS_CODE) {
                    String locationHeader = response.header(LOCATION_HEADER);
                    String targetUrl = locationHeader == null ? originalUrl
                            : "http://" + new URL(originalUrl).getHost() + locationHeader;
                    Timber.d("URL '%s' is redirected to '%s'", originalUrl, targetUrl);
                    subscriber.onNext(targetUrl);
                } else {
                    Timber.w("URL '%s' is not redirected", originalUrl);
                    subscriber.onNext(originalUrl);
                }
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }
    });
}

From source file:com.ayuget.redface.ui.misc.ImageMenuHandler.java

License:Apache License

/**
 * Saves image from network using OkHttp. Picasso is not used because it would strip away the
 * EXIF data once the image is saved (Picasso directly gives us a Bitmap).
 *///from w  w w . java2  s . c o  m
private void saveImageFromNetwork(final File mediaFile, final Bitmap.CompressFormat targetFormat,
        final boolean compressAsPng, final boolean notifyUser, final boolean broadcastSave,
        final ImageSavedCallback imageSavedCallback) {
    OkHttpClient okHttpClient = new OkHttpClient();

    final Request request = new Request.Builder().url(imageUrl).build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            SnackbarHelper.makeError(activity, R.string.error_saving_image).show();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            final byte[] imageBytes = response.body().bytes();

            Timber.d("Image successfully decoded, requesting WRITE_EXTERNAL_STORAGE permission to save image");

            RxPermissions.getInstance(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean granted) {
                            if (granted) {
                                Timber.d("WRITE_EXTERNAL_STORAGE granted, saving image to disk");

                                try {
                                    Timber.d("Saving image to %s", mediaFile.getAbsolutePath());

                                    if (compressAsPng) {
                                        Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0,
                                                imageBytes.length);
                                        StorageHelper.storeImageToFile(bitmap, mediaFile, targetFormat);
                                    } else {
                                        StorageHelper.storeImageToFile(imageBytes, mediaFile);
                                    }

                                    if (broadcastSave) {
                                        // First, notify the system that a new image has been saved
                                        // to external storage. This is important for user experience
                                        // because it makes the image visible in the system gallery
                                        // app.
                                        StorageHelper.broadcastImageWasSaved(activity, mediaFile, targetFormat);
                                    }

                                    if (notifyUser) {
                                        // Then, notify the user with an enhanced snackbar, allowing
                                        // him (or her) to open the image in his favorite app.
                                        Snackbar snackbar = SnackbarHelper.makeWithAction(activity,
                                                R.string.image_saved_successfully,
                                                R.string.action_snackbar_open_image,
                                                new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent();
                                                        intent.setAction(Intent.ACTION_VIEW);
                                                        intent.setDataAndType(
                                                                Uri.parse("file://"
                                                                        + mediaFile.getAbsolutePath()),
                                                                "image/*");
                                                        activity.startActivity(intent);
                                                    }
                                                });
                                        snackbar.show();
                                    }

                                    notifyImageWasSaved(imageSavedCallback, mediaFile, targetFormat);
                                } catch (IOException e) {
                                    Timber.e(e, "Unable to save image to external storage");
                                    SnackbarHelper.makeError(activity, R.string.error_saving_image).show();
                                }
                            } else {
                                Timber.w("WRITE_EXTERNAL_STORAGE denied, unable to save image");
                                SnackbarHelper
                                        .makeError(activity, R.string.error_saving_image_permission_denied)
                                        .show();
                            }
                        }
                    });
        }
    });
}

From source file:com.baasbox.android.net.OkClient.java

License:Apache License

public OkClient() {
    this(new OkHttpClient());
}

From source file:com.baasbox.android.net.OkClient.java

License:Apache License

@Deprecated
public OkClient(BaasBox.Config config) {
    this(new OkHttpClient());
}

From source file:com.baoyz.dribble.AppModule.java

License:Open Source License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();

    try {//from   w  ww .  j a  va  2  s .  c  o m
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        Timber.e(e, "Unable to install disk cache.");
    }

    return client;
}