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.miz.mizuu.MizuuApplication.java

License:Apache License

/**
 * OkHttpClient singleton with 2 MB cache.
 * @return/*from w w w .j  av a 2  s . com*/
 */
public static OkHttpClient getOkHttpClient() {
    if (mOkHttpClient == null) {
        mOkHttpClient = new OkHttpClient();

        File cacheDir = getContext().getCacheDir();
        Cache cache = new Cache(cacheDir, 2 * 1024 * 1024);
        mOkHttpClient.setCache(cache);
    }

    return mOkHttpClient;
}

From source file:com.mobimvp.cliques.service.RequestManager.java

License:Apache License

private static RequestQueue newRequestQueue() {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.networkInterceptors().add(new StethoInterceptor());
    RequestQueue requestQueue = new RequestQueue(openCache(), new BasicNetwork(new OkHttpStack(okHttpClient)));
    requestQueue.start();/*from   w  w  w. j  a  va 2s  .  c  o  m*/
    return requestQueue;
}

From source file:com.mtnfog.edd.licensing.api.EDDLicensingClientFactory.java

License:Apache License

/**
 * Creates a REST client.//from   w w  w .  j a v a2s . com
 * @param url The URL of the EDD Licensing REST API endpoint.
 * @return A {@link EDDLicensingClient}.
 */
public static EDDLicensingClient createRestClient(String url) {

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(url).setConverter(new GsonConverter(gson))
            .setClient(new OkClient(new OkHttpClient())).build();

    EDDLicensingClient client = restAdapter.create(EDDLicensingClient.class);

    return client;

}

From source file:com.mtnfog.idyl.e3.sdk.IdylE3ClientFactory.java

License:Apache License

private static RestAdapter getAuthenticatedRestAdapter(String endpoint, String apiKey,
        AuthenticationMethod method) {//from  w  w  w . j  a  v a  2 s . c  o  m

    final String requestDate = getRequestDate();

    RequestInterceptor requestInterceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {

            request.addHeader("Content-Type", "application/json");
            request.addHeader("Accept", "application/json");
            request.addHeader("x-idyle3-date", requestDate);

        }

    };

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(new HmacSha512v1SigningInterceptor(apiKey, method, requestDate));

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
            .setConverter(new GsonConverter(new Gson())).setLogLevel(RestAdapter.LogLevel.FULL)
            .setRequestInterceptor(requestInterceptor).setClient(new OkClient(okHttpClient)).setLog(new Log() {

                @Override
                public void log(String message) {
                    LOGGER.debug(message);
                }

            }).build();

    return restAdapter;

}

From source file:com.mtnfog.metatext.MetaTextStandardClient.java

License:Apache License

/**
 * Initialize a new client with your API key.
 * @param apiKey Your MetaText API key.// w  w w. ja  v a  2  s  .c o m
 */
public MetaTextStandardClient(String apiKey, String endpoint) {

    this.apiKey = apiKey;

    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(120, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(120, TimeUnit.SECONDS);

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
            .setConverter(new GsonConverter(new Gson())).setClient(new OkClient(okHttpClient)).build();

    service = restAdapter.create(MetaTextService.class);

}

From source file:com.mvcoding.financius.api.ApiModule.java

License:Open Source License

@Provides
@Singleton/*from w w  w  . jav  a 2  s  .  c o  m*/
public RestAdapter provideRestAdapter(EndpointAuthenticator endpointAuthenticator,
        EndpointInterceptor endpointInterceptor) {
    final OkHttpClient httpClient = new OkHttpClient();
    httpClient.setAuthenticator(endpointAuthenticator);
    httpClient.setConnectTimeout(20, TimeUnit.SECONDS);
    httpClient.setReadTimeout(20, TimeUnit.SECONDS);
    httpClient.setWriteTimeout(20, TimeUnit.SECONDS);

    return new RestAdapter.Builder().setClient(new OkClient(httpClient)).setEndpoint(ENDPOINT)
            .setRequestInterceptor(endpointInterceptor).setLogLevel(RestAdapter.LogLevel.FULL).build();
}

From source file:com.mycelium.wallet.external.cashila.api.CashilaService.java

License:Microsoft Reference Source License

public CashilaService(String baseUrl, String apiVersion, Bus eventBus) {
    this.baseUrl = baseUrl;
    this.eventBus = eventBus;

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(5000, TimeUnit.MILLISECONDS);
    client.setReadTimeout(5000, TimeUnit.MILLISECONDS);
    client.networkInterceptors().add(hmacInterceptor);
    CertificatePinner certPinner = new CertificatePinner.Builder().add("cashila.com", CASHILA_CERT)
            .add("cashila-staging.com", CASHILA_CERT).build();
    client.setCertificatePinner(certPinner);

    // use jackson as json mapper, as we already use it in the rest of the project
    objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    objectMapper.registerModule(new WapiJsonModule());

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(baseUrl + apiVersion + "/")
            .setLogLevel(RestAdapter.LogLevel.BASIC)
            //.setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new JacksonConverter(objectMapper)).setClient(new OkClient(client))
            .setRequestInterceptor(apiIdInterceptor).build();

    cashila = adapter.create(Cashila.class);

    // initialise nonce with current time and increase by one on each call
    lastNonce = System.currentTimeMillis();
}

From source file:com.nabilhachicha.kc.di.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS);
    client.setReadTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS);

    // Install an HTTP cache in the application cache directory.
    try {/*w ww  .  j a  v a2s . c  o  m*/
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return client;
}

From source file:com.navercorp.pinpoint.plugin.okhttp.OkHttpClientBackwardCompatibilityIT.java

License:Apache License

@Test
public void execute() throws Exception {
    Request request = new Request.Builder().url("http://google.com").build();
    OkHttpClient client = new OkHttpClient();
    Response response = client.newCall(request).execute();

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();//from  www . j  ava 2 s  .  c om

    Method callMethod = Call.class.getDeclaredMethod("execute");
    verifier.verifyTrace(Expectations.event("OK_HTTP_CLIENT_INTERNAL", callMethod));
}

From source file:com.nbonnec.mediaseb.di.modules.ApiModule.java

License:Apache License

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

    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT));

    try {/*w ww  .  j a v a2 s .co  m*/
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (NullPointerException e) {
        Log.e(TAG, "Unable to initialize OkHttpclient with disk cache", e);
    }

    return client;
}