List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.tsums.newshacker.core.NHModule.java
License:Open Source License
@Provides @Singleton OkHttpClient provideOkClient() { OkHttpClient client = new OkHttpClient(); return client; }
From source file:com.twolinessoftware.network.BaseRetrofitHelper.java
License:Apache License
public BaseApiService newBaseApiService(String endpoint) { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS); RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint) .setClient(new OkClient(okHttpClient)).setRequestInterceptor(apiRequestInterceptor) .setLogLevel(Config.RETROFIT_LOGLEVEL).setErrorHandler(apiErrorConverter) .setConverter(new GsonConverter(GsonUtil.buildGsonAdapter())).build(); return restAdapter.create(BaseApiService.class); }
From source file:com.uber.sdk.rides.client.internal.RetrofitUberRidesClient.java
License:Open Source License
/** * Builds a RestAdapter./* w ww . ja va2 s . c o m*/ */ private static RestAdapter buildRestAdapter(final Session session, String endpointHost, final OAuth2Helper oAuth2Helper, RestAdapter.LogLevel logLevel, OkHttpClient httpClient) throws IOException { RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade requestFacade) { Credential credential = session.getCredential(); if (credential != null) { oAuth2Helper.refreshCredentialIfNeeded(credential); requestFacade.addHeader("Authorization", "Bearer " + credential.getAccessToken()); } else { requestFacade.addHeader("Authorization", "Token " + session.getServerToken()); } if (session.getLocale() != null) { requestFacade.addHeader("Accept-Language", session.getLocale().getLanguage()); } requestFacade.addHeader("X-Uber-User-Agent", "Java Rides SDK v" + LIB_VERSION); } }; if (httpClient == null) { httpClient = new OkHttpClient(); httpClient.setConnectTimeout(1, TimeUnit.MINUTES); httpClient.setReadTimeout(1, TimeUnit.MINUTES); httpClient.setFollowRedirects(false); httpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request oldRequest = chain.request(); Response response = chain.proceed(oldRequest); if (response.isRedirect()) { String redirectUrl = response.header(HttpHeaders.LOCATION); Request newRequest = oldRequest.newBuilder().url(redirectUrl).build(); return chain.proceed(newRequest); } return response; } }); } return new RestAdapter.Builder().setEndpoint(endpointHost) .setConverter(new GsonConverter(new GsonBuilder().create())) .setRequestInterceptor(requestInterceptor).setClient(new OkClient(httpClient)).setLogLevel(logLevel) .build(); }
From source file:com.uber.sdk.rides.client.internal.RetrofitUberRidesClientIntegrationTest.java
License:Open Source License
@Before public void setUp() throws Exception { server = new MockWebServer(); server.start();/*from w ww . ja v a2 s . co m*/ endpointHost = server.url("").toString(); credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()) .setClientAuthentication(new ClientParametersAuthentication("CLIENT_ID", "CLIENT_SECRET")) .setTokenServerUrl(new GenericUrl(endpointHost)).build(); credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(REFRESH_TOKEN); credential.setExpiresInSeconds(3600L); sandboxSession = new Session.Builder().setCredential(credential).setEnvironment(SANDBOX).build(); okHttpClient = new OkHttpClient(); uberApiAsyncService = RetrofitUberRidesClient.getUberApiService(sandboxSession, new OAuth2Helper(), UberRidesServices.LogLevel.FULL, endpointHost, okHttpClient, RetrofitUberRidesService.class); uberApiSyncService = (UberRidesSyncService) uberApiAsyncService; }
From source file:com.uditgupta.udacity.popularmovies.data.DataModule.java
License:Apache License
static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, SECONDS); client.setReadTimeout(10, SECONDS);//w w w. j av a 2 s . c o m client.setWriteTimeout(10, SECONDS); File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); return client; }
From source file:com.uphyca.robota.RobotaModule.java
License:Apache License
@Provides @Singleton/*from w w w.ja v a 2 s . c o m*/ OkHttpClient provideOkHttpClient(CookieHandler cookieHandler) { OkHttpClient okHttpClient = new OkHttpClient(); File cacheDir = new File(mApplication.getCacheDir(), "okhttp"); final HttpResponseCache cache; try { cache = new HttpResponseCache(cacheDir, 10 * 1024 * 1024); } catch (IOException e) { throw new IllegalStateException(e); } okHttpClient.setResponseCache(cache).setCookieHandler(cookieHandler); return okHttpClient; }
From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java
License:Apache License
public Response doRest(String path, String requestBody, HttpVerb verb) throws IOException, RestApiException { OkHttpClient client = new OkHttpClient(); Optional<String> gerritAuthOptional = updateGerritAuthWhenRequired(client); String uri = authData.getHost(); // only use /a when http login is required (i.e. we haven't got a gerrit-auth cookie) // it would work in most cases also with /a, but it breaks with HTTP digest auth ("Forbidden" returned) if (authData.isLoginAndPasswordAvailable() && !gerritAuthOptional.isPresent()) { uri += "/a"; }/* w ww .j a va 2s .co m*/ uri += path; Request.Builder builder = new Request.Builder().url(uri).addHeader("Accept", MEDIA_TYPE_JSON.toString()); if (verb == HttpVerb.GET) { builder = builder.get(); } else if (verb == HttpVerb.DELETE) { builder = builder.delete(); } else { if (requestBody == null) { builder.method(verb.toString(), null); } else { builder.method(verb.toString(), RequestBody.create(MEDIA_TYPE_JSON, requestBody)); } } if (gerritAuthOptional.isPresent()) { builder.addHeader("X-Gerrit-Auth", gerritAuthOptional.get()); } return httpRequestExecutor.execute(client, builder); }
From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java
License:Apache License
private HttpClientBuilder getHttpClient(HttpContext httpContext) { HttpClientBuilder client = HttpClients.custom(); client.useSystemProperties(); // see also: com.intellij.util.net.ssl.CertificateManager OkHttpClient c = new OkHttpClient(); c.setFollowRedirects(true);/*from w w w.jav a 2 s . c o m*/ // we need to get redirected result after login (which is done with POST) for extracting xGerritAuth client.setRedirectStrategy(new LaxRedirectStrategy()); c.setCookieHandler(cookieManager); c.setConnectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS); c.setReadTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS); c.setWriteTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS); CredentialsProvider credentialsProvider = getCredentialsProvider(); client.setDefaultCredentialsProvider(credentialsProvider); if (authData.isLoginAndPasswordAvailable()) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(authData.getLogin(), authData.getPassword())); BasicScheme basicAuth = new BasicScheme(); httpContext.setAttribute(PREEMPTIVE_AUTH, basicAuth); client.addInterceptorFirst(new PreemptiveAuthHttpRequestInterceptor(authData)); } client.addInterceptorLast(new UserAgentHttpRequestInterceptor()); for (HttpClientBuilderExtension httpClientBuilderExtension : httpClientBuilderExtensions) { client = httpClientBuilderExtension.extend(client, authData); credentialsProvider = httpClientBuilderExtension.extendCredentialProvider(client, credentialsProvider, authData); } return client; }
From source file:com.ushahidi.android.presentation.di.modules.AppModule.java
License:Open Source License
private static OkHttpClient createOkHttpClient(Context app) { OkHttpClient client = new OkHttpClient(); File cacheDir = new File(app.getApplicationContext().getCacheDir(), "ushahidi-android-http-cache"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache);//from w ww .java 2 s . c om return client; }
From source file:com.ushahidi.android.test.presenter.ListPostPresenterTest.java
License:Open Source License
@Override protected void setUp() throws Exception { super.setUp(); MockitoAnnotations.initMocks(this); mMockApiServiceUtil = new ApiServiceUtil(new OkClient(new OkHttpClient())); setupPrefsMock();//from w ww .j a v a 2s .c o m mPostListPresenter = new ListPostPresenter(mMockListPost, mMockFetchPost, mMockPostModelDataMapper, mMockPostEntityMapper, mMockPostDatabaseHelper, mMockPrefs, mMockApiServiceUtil, mMockContext); //mPostListPresenter.setPostService(mPostService); mPostListPresenter.setView(mMockView); }