List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.github.leonardoxh.temporeal.model.listeners.PushSender.java
License:Apache License
public static void send(String entity) { Request.Builder requestBuilder = new Request.Builder(); requestBuilder.url(ApplicationConfiguration.PUSH_SERVICE_URL); requestBuilder// ww w . j a v a2 s.com .post(RequestBody.create(MediaType.parse("application/json"), getJsonOfRegistrationsId(entity))); requestBuilder.header("Authorization", ApplicationConfiguration.GCM_PROJECT_ID); OkHttpClient okHttp = new OkHttpClient(); try { Response response = okHttp.newCall(requestBuilder.build()).execute(); System.out.println("Response code: " + response.code()); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.github.leonardoxh.ywheaterdata.Utils.java
License:Apache License
static OkHttpClient defaultOkHttpClient() { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_UNIT); okHttpClient.setReadTimeout(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_UNIT); okHttpClient.setWriteTimeout(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_UNIT); return okHttpClient; }
From source file:com.github.mmatczuk.ablb.benchmark.OkHttpAsync.java
License:Apache License
@Override public void prepare(final Benchmark benchmark) { concurrencyLevel = benchmark.concurrencyLevel; targetBacklog = benchmark.targetBacklog; client = new OkHttpClient(); client.setDispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))); callback = new Callback() { @Override/*from www .jav a2 s . c o m*/ public void onFailure(Request request, IOException e) { System.out.println("Failed: " + e); } @Override public void onResponse(Response response) throws IOException { ResponseBody body = response.body(); long total = readAllAndClose(body.byteStream()); long finish = System.nanoTime(); if (VERBOSE) { long start = (Long) response.request().tag(); System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start)); } requestsInFlight.decrementAndGet(); } }; }
From source file:com.github.mobile.DefaultClient.java
License:Apache License
/** * Create connection to URI/* w w w .j a va 2 s . c om*/ * * @param uri * @return connection * @throws IOException */ @Override protected HttpURLConnection createConnection(String uri) throws IOException { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) { return super.createConnection(uri); } OkUrlFactory factory = new OkUrlFactory(new OkHttpClient()); URL url = new URL(createUri(uri)); return factory.open(url); }
From source file:com.github.mobile.util.AvatarLoader.java
License:Apache License
/** * Create avatar helper//w w w . j av a 2 s . com * * @param context */ @Inject public AvatarLoader(final Context context) { this.context = context; OkHttpClient client = new OkHttpClient(); // Install an HTTP cache in the application cache directory. File cacheDir = new File(context.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); p = new Picasso.Builder(context).downloader(new OkHttpDownloader(client)).build(); float density = context.getResources().getDisplayMetrics().density; cornerRadius = CORNER_RADIUS_IN_DIP * density; if (avatarSize == 0) { avatarSize = getMaxAvatarSize(context); } // TODO remove this eventually // Delete the old cache final File avatarDir = new File(context.getCacheDir(), "avatars/github.com"); if (avatarDir.isDirectory()) deleteCache(avatarDir); }
From source file:com.github.mobile.util.HttpImageGetter.java
License:Apache License
@Override public Drawable getDrawable(final String source) { try {//from w ww . j a v a 2s . c om Drawable repositoryImage = requestRepositoryImage(source); if (repositoryImage != null) return repositoryImage; } catch (Exception e) { // Ignore and attempt request over regular HTTP request } if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) { return loading.getDrawable(source); } try { Request request = new Request.Builder().url(source).build(); OkHttpClient client = new OkHttpClient(); Response response = client.newCall(request).execute(); Bitmap bitmap = ImageUtils.getBitmap(response.body().bytes(), width, MAX_VALUE); if (bitmap == null) return loading.getDrawable(source); BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); return drawable; } catch (IOException e) { return loading.getDrawable(source); } }
From source file:com.github.obsessive.library.utils.VolleyHelper.java
License:Apache License
/** * init volley helper//from w w w. j a va 2 s .c o m * * @param context */ public void init(Context context) { OkHttpClient okHttpClient = new OkHttpClient(); requestQueue = Volley.newRequestQueue(context, new OkHttpStack(okHttpClient)); }
From source file:com.github.pockethub.android.util.HttpImageGetter.java
License:Apache License
/** * Create image getter for context// w w w .jav a2 s. c o m * * @param context */ @Inject public HttpImageGetter(Context context) { this.context = context; dir = context.getCacheDir(); width = ServiceUtils.getDisplayWidth(context); loading = new LoadingImageGetter(context, 24); okHttpClient = new OkHttpClient(); }
From source file:com.github.pockethub.util.HttpImageGetter.java
License:Apache License
/** * Create image getter for context/* w w w. j a va 2 s . co m*/ * * @param context * @param service */ @Inject public HttpImageGetter(Context context, ContentsService service) { this.context = context; this.service = service; dir = context.getCacheDir(); width = ServiceUtils.getDisplayWidth(context); loading = new LoadingImageGetter(context, 24); okHttpClient = new OkHttpClient(); }
From source file:com.github.radium226.github.maven.AssetWagon.java
License:Apache License
@Override public void doConnect(Optional<AuthenticationInfo> authenticationInfo) throws ConnectionException { try {//from w w w . j av a 2 s . c om // GitHub Authentication GitHubBuilder gitHubBuilder = new GitHubBuilder(); if (authenticationInfo.isPresent()) { String login = authenticationInfo.get().getUserName(); String password = authenticationInfo.get().getPassword(); if (password.startsWith("oauth2_token:")) { gitHubBuilder = gitHubBuilder.withOAuthToken(password.substring("oauth2_otken:".length())); } else { gitHubBuilder = gitHubBuilder.withPassword(login, password); } } // Proxy Authentication okHttpClient = new OkHttpClient(); ProxyInfo proxyInfo = getProxyInfoByType("http"); if (proxyInfo != null) { okHttpClient.setProxy(ProxyInfos.asProxy(proxyInfo)); final String userName = proxyInfo.getUserName(); final String password = proxyInfo.getPassword(); if (userName != null && password != null) { okHttpClient.setAuthenticator(new Authenticator() { @Override public Request authenticateProxy(Proxy proxy, Response response) { return response.request(); } @Override public Request authenticate(Proxy proxy, Response response) throws IOException { String credentials = Credentials.basic(userName, password); return response.request().newBuilder().header("Proxy-Authorization", credentials) .build(); } }); } } gitHub = gitHubBuilder.withConnector(new OkHttpConnector(new OkUrlFactory(okHttpClient))).build(); this.gitHubService = GitHubService.forGitHub(gitHub).withHttpClient(okHttpClient); GHMyself myself = gitHub.getMyself(); LOGGER.info("Connected as {}", myself.getName()); } catch (IOException e) { throw new ConnectionException("Unable to connect to GitHub", e); } }