List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:javajo.sample.codic.Codic.java
License:Apache License
public List<Translation> translate(String text) { URL url = new HttpUrl.Builder().scheme("https").host("api.codic.jp").addPathSegment("v1") .addPathSegment("engine").addPathSegment("translate.json").addQueryParameter("text", text).build() .url();//from w ww . ja v a2 s. co m Request request = new Builder().url(url).addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken).get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException( "Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() { }); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } }
From source file:keywhiz.cli.ClientUtils.java
License:Apache License
/** * Creates a {@link OkHttpClient} to start a TLS connection. * * @param devTrustStore if not null, uses the provided TrustStore instead of whatever is * configured in the JVM. This is a convenient way to allow developers to * start playing with Keywhiz right away. This option should not be used in * production systems. * @param cookies list of cookies to include in the client. * @return new http client./*from w ww . j a va 2 s.c om*/ */ public static OkHttpClient sslOkHttpClient(@Nullable KeyStore devTrustStore, List<HttpCookie> cookies) { checkNotNull(cookies); SSLContext sslContext; try { TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(devTrustStore); TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(new KeyManager[0], trustManagers, new SecureRandom()); } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { throw Throwables.propagate(e); } SSLSocketFactory socketFactory = sslContext.getSocketFactory(); OkHttpClient client = new OkHttpClient().setSslSocketFactory(socketFactory) .setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).setFollowSslRedirects(false); client.setRetryOnConnectionFailure(false); client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN")); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); cookies.forEach(c -> cookieManager.getCookieStore().add(null, c)); client.setCookieHandler(cookieManager); return client; }
From source file:m.m.com.m.core.service.AbstractService.java
License:Apache License
public void runOnUiThread() { try {/*from w w w. jav a 2 s . c o m*/ new OkHttpClient().newCall(mRequest).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { mOnRequestResponse.onFail(-2); } @Override public void onResponse(Response response) throws IOException { mOnRequestResponse.onSucess(response.body().string()); } }); } catch (Exception e) { e.printStackTrace(); } }
From source file:m.m.com.m.core.service.AbstractService.java
License:Apache License
@Override protected String doInBackground(Request... request) { try {/*w w w.j a v a 2 s . c o m*/ return new OkHttpClient().newCall(request[0]).execute().body().string(); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:me.drakeet.meizhi.DrakeetRetrofit.java
License:Open Source License
DrakeetRetrofit() { OkHttpClient client = new OkHttpClient(); client.setReadTimeout(12, TimeUnit.SECONDS); RestAdapter.Builder builder = new RestAdapter.Builder(); builder.setClient(new OkClient(client)).setLogLevel(RestAdapter.LogLevel.FULL) .setEndpoint("http://gank.io/api").setConverter(new GsonConverter(gson)); RestAdapter gankRestAdapter = builder.build(); builder.setEndpoint("https://leancloud.cn:443/1.1/classes"); RestAdapter drakeetRestAdapter = builder.build(); gankService = gankRestAdapter.create(GankApi.class); drakeetService = drakeetRestAdapter.create(DrakeetApi.class); }
From source file:me.drakeet.meizhi.ui.GankFragment.java
License:Open Source License
private void loadVideoPreview() { String where = String.format("{\"tag\":\"%d-%d-%d\"}", mYear, mMonth, mDay); DrakeetFactory.getDrakeetSingleton().getDGankData(where).map(dGankData -> dGankData.results) .single(dGanks -> dGanks.size() > 0).map(dGanks -> dGanks.get(0)) .observeOn(AndroidSchedulers.mainThread()).subscribe(dGank -> startPreview(dGank.preview), throwable -> getOldVideoPreview(new OkHttpClient())); }
From source file:me.hoangchunghien.popularmovies.data.api.ApiModule.java
License:Apache License
@Provides @Singleton/*from w w w. ja v a2 s . c om*/ OkHttpClient provideOkHttpClient(@ForApplication Context app) { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, SECONDS); client.setReadTimeout(10, SECONDS); client.setWriteTimeout(10, SECONDS); // Install an HTTP cache in the application cache directory. File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); return client; }
From source file:me.lock8.Mzigo.java
License:Apache License
public Mzigo() { this(new OkHttpClient(), DUPLICATED_REQUEST_POLICY_WAIT_FOR_RESPONSE); }
From source file:me.rhespanhol.instalib.ServiceUtils.java
License:Apache License
/** * Returns this apps {@link OkHttpClient} with enabled response cache. * Should be used with API calls./*from ww w . j av a 2 s .c o m*/ */ public static synchronized OkHttpClient getCachingOkHttpClient(Context context) { if (cachingHttpClient == null) { cachingHttpClient = new OkHttpClient(); cachingHttpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); cachingHttpClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); File cacheDir = createApiCacheDir(context); cachingHttpClient.setCache(new Cache(cacheDir, calculateApiDiskCacheSize(cacheDir))); } return cachingHttpClient; }
From source file:meteor.operations.Meteor.java
License:Apache License
/** * Opens a connection to the server over websocket * * @param isReconnect whether this is a re-connect attempt or not *///from w ww. j a v a2s . c om public void openConnection(final boolean isReconnect) { if (isReconnect) { if (isConnected()) { connect(mSessionID); return; } } OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(1, TimeUnit.MINUTES); client.setReadTimeout(1, TimeUnit.MINUTES); client.setWriteTimeout(1, TimeUnit.MINUTES); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); client.networkInterceptors().add(loggingInterceptor); Request request = new Request.Builder().url(mServerUri).build(); WebSocketCall.create(client, request).enqueue(mWebSocketObserver); }