List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.github.retrofit2.GitHubOkHttpClienter.java
License:Apache License
@Override public OkHttpClient get() { return new OkHttpClient(); }
From source file:com.github.sebhoss.identifier.client.IdentifierClients.java
License:Open Source License
/** * @param baseUrl//from w w w. ja v a 2 s. c o m * The remote base-URL to talk to. Since this is used as the basis to construct URLs, the given base-URL * is required to end in a "/" * @return A new client facade which talks to the given baseUrl */ public static IdentifierClient http(final String baseUrl) { return http(new OkHttpClient(), baseUrl); }
From source file:com.github.sebhoss.identifier.client.IdentifierClientsTest.java
License:Open Source License
/** * Ensures that a custom HTTP client with a custom URL can be built. *///from ww w .j a v a2 s . co m @Test public void shouldBuildCustomHttpClientWithCustomURL() { // given final OkHttpClient ok = new OkHttpClient(); final String url = "example.com"; //$NON-NLS-1$ // when final IdentifierClient client = IdentifierClients.http(ok, url); // then Assert.assertNotNull(client); }
From source file:com.github.shredder121.gh_event_api.handler.AbstractHandlerTest.java
License:Apache License
private static GitHub getGitHub() throws IOException { OkHttpClient client = new OkHttpClient(); client.setCache(new Cache(Paths.get(".", ".cache").toFile(), FileUtils.ONE_MB * 10)); return new GitHubBuilder().withConnector(new RawGitOkHttpConnector(new OkUrlFactory(client))).build(); }
From source file:com.github.zunix.ryoshi.api.resources.RootResourceTest.java
License:Open Source License
@Before public void initialize() { clientID = ""; accessToken = ""; ryoshi = new Ryoshi(clientID, accessToken, false); httpClient = new OkHttpClient(); }
From source file:com.gittalent.service.GithubImportService.java
License:Apache License
public GitHub initGithub() { String tmpDirPath = System.getProperty("java.io.tmpdir"); File cacheDirectoryParent = new File(tmpDirPath); File cacheDirectory = new File(cacheDirectoryParent, "okhttpCache"); if (!cacheDirectory.exists()) { cacheDirectory.mkdir();//from ww w . j a va 2 s .c om } Cache cache = new Cache(cacheDirectory, 100 * 1024 * 1024); try { return GitHubBuilder.fromCredentials().withRateLimitHandler(RateLimitHandler.WAIT) .withAbuseLimitHandler(AbuseLimitHandler.WAIT) .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)))) .build(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.goforer.base.module.BaseGlideModule.java
License:Apache License
@Override public void registerComponents(Context context, Glide glide) { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); }
From source file:com.google.android.apps.muzei.util.IOUtil.java
License:Apache License
public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring) throws OpenUriException { if (uri == null) { throw new IllegalArgumentException("Uri cannot be empty"); }/*from w w w. j a v a 2 s . c o m*/ String scheme = uri.getScheme(); if (scheme == null) { throw new OpenUriException(false, new IOException("Uri had no scheme")); } InputStream in = null; if ("content".equals(scheme)) { try { in = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException | SecurityException e) { throw new OpenUriException(false, e); } } else if ("file".equals(scheme)) { List<String> segments = uri.getPathSegments(); if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) { AssetManager assetManager = context.getAssets(); StringBuilder assetPath = new StringBuilder(); for (int i = 1; i < segments.size(); i++) { if (i > 1) { assetPath.append("/"); } assetPath.append(segments.get(i)); } try { in = assetManager.open(assetPath.toString()); } catch (IOException e) { throw new OpenUriException(false, e); } } else { try { in = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } } } else if ("http".equals(scheme) || "https".equals(scheme)) { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = null; int responseCode = 0; String responseMessage = null; try { conn = new OkUrlFactory(client).open(new URL(uri.toString())); } catch (MalformedURLException e) { throw new OpenUriException(false, e); } try { conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); responseCode = conn.getResponseCode(); responseMessage = conn.getResponseMessage(); if (!(responseCode >= 200 && responseCode < 300)) { throw new IOException("HTTP error response."); } if (reqContentTypeSubstring != null) { String contentType = conn.getContentType(); if (contentType == null || !contentType.contains(reqContentTypeSubstring)) { throw new IOException("HTTP content type '" + contentType + "' didn't match '" + reqContentTypeSubstring + "'."); } } in = conn.getInputStream(); } catch (IOException e) { if (responseCode > 0) { throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e); } else { throw new OpenUriException(false, e); } } } return in; }
From source file:com.google.android.apps.muzei.util.IOUtil.java
License:Apache License
static String fetchPlainText(URL url) throws IOException { InputStream in = null;/* ww w. j a v a 2 s . co m*/ try { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = new OkUrlFactory(client).open(url); conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); in = conn.getInputStream(); return readFullyPlainText(in); } finally { if (in != null) { in.close(); } } }
From source file:com.google.android.gcm.demo.app.DemoActivity.java
License:Apache License
private boolean registerToUniqush(String registrationId) throws IOException { RequestBody formBody = new FormEncodingBuilder().add("service", "ZenUI_PushService_5") .add("subscriber", "uniqush.client").add("pushservicetype", "gcm").add("regid", registrationId) .build();// ww w .j av a 2 s. c o m String url = "http://140.128.101.214:9898/subscribe"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).post(formBody).build(); Response response = client.newCall(request).execute(); return response.isSuccessful(); }