List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.inductiveautomation.ignition.examples.reporting.datasource.common.RestJsonDataSource.java
/** * Attempts to connect to a Url using {@link OkHttpClient} and return the body of the reply as a String, if it is * JSON mime type./*www . jav a2s.c o m*/ * @param url String specifying the * @return the body of the http response received from the specified Url */ private String collectData(String url) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); // get headers to check the content type out of Headers headers = null; if (response != null) { headers = response.headers(); } // at least try to make sure we have JSON data. boolean isJSON = false; if (headers != null) { isJSON = MediaType.parse(headers.get("Content-Type")).equals(JSON); } return isJSON ? response.body().string() : ""; }
From source file:com.it1224.demos.psretrofit.utils.OkHttpUtils.java
License:Open Source License
public static OkHttpClient getInstance(Context context) { if (singleton == null) { synchronized (OkHttpUtils.class) { if (singleton == null) { File cacheDir = new File(context.getCacheDir(), "rcache"); singleton = new OkHttpClient(); try { //singleton.setCache(null); // singleton.setCache(new Cache(cacheDir, 1024 * 1024 * 10));//?? } catch (Exception e) { e.printStackTrace(); }//from w w w . j a v a 2 s. co m // singleton.setConnectTimeout(1000 * 30, TimeUnit.MILLISECONDS); // singleton.setReadTimeout(1000 * 30, TimeUnit.MILLISECONDS); } } } return singleton; }
From source file:com.jaspersoft.android.jaspermobile.internal.di.modules.app.AppModule.java
License:Open Source License
@Singleton @Named("webview_client") @Provides/*from w w w .j a va 2s . c o m*/ OkHttpClient provideWebViewClient(@ApplicationContext Context context) { OkHttpClient client = new OkHttpClient(); File cacheDir = context.getApplicationContext().getCacheDir(); File okCache = new File(cacheDir, "ok-cache"); if (!okCache.exists()) { boolean cachedCreated = okCache.mkdirs(); if (cachedCreated) { int cacheSize = 50 * 1024 * 1024; Cache cache = new Cache(okCache, cacheSize); client.setCache(cache); } } return client; }
From source file:com.joeyfrazee.nifi.reporting.HttpProvenanceReporter.java
License:Apache License
@OnScheduled public void setup(final ConfigurationContext context) throws IOException { client.set(new OkHttpClient()); }
From source file:com.kwido.fiware.android.sdk.ServiceGenerator.java
License:Apache License
public static <S> S createService(Class<S> serviceClass, String endpoint, final String token) { // set endpoint url and use OkHTTP as HTTP client RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(endpoint) .setConverter(new JacksonConverter()) .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.BASIC) .setClient(new OkClient(new OkHttpClient())); builder.setRequestInterceptor(new RequestInterceptor() { @Override/*ww w . j av a2 s .co m*/ public void intercept(RequestFacade request) { request.addHeader(Constants.HEADER_ACCEPT, "application/json"); if (token != null) { request.addHeader(Constants.HEADER_TOKEN, token); } } }); RestAdapter adapter = builder.build(); return adapter.create(serviceClass); }
From source file:com.liferay.mobile.android.auth.CookieSignIn.java
License:Open Source License
protected Call signIn() throws Exception { if (!(session.getAuthentication() instanceof CookieAuthentication)) { throw new Exception("Can't sign in if authentication implementation is not " + "CookieAuthentication"); }// w w w . java2s . c om CookieAuthentication cookieAuthentication = getCookieAuthentication(session.getAuthentication()); username = cookieAuthentication.getUsername(); password = cookieAuthentication.getPassword(); cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); OkHttpClient client = new OkHttpClient(); Authenticator authenticator = authenticators.get(session.getServer()); if (authenticator != null) { client.setAuthenticator(authenticator); } client.setCookieHandler(cookieManager); client.setFollowRedirects(true); Builder builder = getBuilder(session, username, password); return client.newCall(builder.build()); }
From source file:com.liferay.mobile.android.http.client.OkHttpClientImpl.java
License:Open Source License
public OkHttpClientImpl() { client = new OkHttpClient(); }
From source file:com.liferay.mobile.screens.context.LiferayServerContext.java
License:Open Source License
public static OkHttpClient getOkHttpClient() { synchronized (LiferayServerContext.class) { if (okHttpClient == null) { okHttpClient = new OkHttpClient(); okHttpClient.setCache(new Cache(LiferayScreensContext.getContext().getCacheDir(), MAX_SIZE)); }// w w w . j a v a 2 s .co m } return okHttpClient; }
From source file:com.liferay.mobile.sdk.auth.CookieSignIn.java
License:Open Source License
public static void signIn(Config config, CookieCallback callback) { try {/* w w w . ja v a 2s . com*/ Authentication auth = config.auth(); if (!(auth instanceof BasicAuthentication)) { throw new Exception( "Can't sign in if authentication implementation is not " + "BasicAuthentication"); } OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); client.setFollowRedirects(true); Builder builder = new Builder(); MediaType contentType = MediaType.parse("application/x-www-form-urlencoded"); builder.post(RequestBody.create(contentType, getBody((BasicAuthentication) auth))); builder.addHeader("Cookie", "COOKIE_SUPPORT=true;"); builder.url(getLoginURL(config.server())); Call call = client.newCall(builder.build()); call.enqueue(getCallback(callback, cookieManager)); } catch (Exception e) { callback.onFailure(e); } }
From source file:com.liferay.mobile.sdk.SDKBuilder.java
License:Open Source License
public Discovery discover(String url, String context, int portalVersion) throws Exception { if ("portal".equals(context)) { context = ""; }/*from w ww . j a v a 2 s. co m*/ if (portalVersion == 62) { if (Validator.isNotNull(context)) { context = "/" + context; } url = String.format("%s%s/api/jsonws?discover", url, context); } else if (portalVersion == 7) { url = String.format("%s/api/jsonws?discover&contextName=%s", url, context); } OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("Unexpected HTTP response: " + response); } if (portalVersion == 7) { JSONParser.registerTypeAdapter(Discovery.class, new DiscoveryDeserializer()); JSONParser.registerTypeAdapter(Action.class, new ActionDeserializer()); } return JSONParser.fromJSON(response.body().string(), Discovery.class); }