List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.example.app.di.modules.ApiModule.java
License:Apache License
@Provides @Singleton//from w w w.j a v a 2 s. c o m @Named("cache") OkHttpClient provideOkHttpClient(Cache cache, HttpLoggingInterceptor loggingInterceptor, @Named("userAgent") String userAgentValue) { OkHttpClient client = new OkHttpClient(); client.setCache(cache); client.interceptors().add(loggingInterceptor); client.networkInterceptors().add(new UserAgentInterceptor(userAgentValue)); client.networkInterceptors().add(new CacheResponseInterceptor()); client.networkInterceptors().add(new StethoInterceptor()); return client; }
From source file:com.example.app.utils.StethoOkHttpGlideModule.java
License:Apache License
@Override public void registerComponents(Context context, Glide glide) { OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(new StethoInterceptor()); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); }
From source file:com.example.jordan.sunshine.app.FetchWeatherTask.java
License:Apache License
@Override protected Void doInBackground(String... params) { // If there's no zip code, there's nothing to look up. Verify size of params. if (params.length == 0) { return null; }/*from w w w . j av a 2s. com*/ String locationQuery = params[0]; // These two need to be declared outside the try/catch // so that they can be closed in the finally block. // HttpURLConnection urlConnection = null; // BufferedReader reader = null; // Will contain the raw JSON response as a string. String forecastJsonStr = null; String format = "json"; String units = "metric"; int numDays = 14; // Construct the URL for the OpenWeatherMap query // Possible parameters are avaiable at OWM's forecast API page, at // http://openweathermap.org/API#forecast // Ex URL: http://api.openweathermap.org/data/2.5/forecast/daily?q=92691&mode=json&units=metric&cnt=7 final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?"; final String QUERY_PARAM = "q"; final String FORMAT_PARAM = "mode"; final String UNITS_PARAM = "units"; final String DAYS_PARAM = "cnt"; Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon().appendQueryParameter(QUERY_PARAM, params[0]) .appendQueryParameter(FORMAT_PARAM, format).appendQueryParameter(UNITS_PARAM, units) .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays)).build(); try { URL url = new URL(builtUri.toString()); OkHttpClient client = new OkHttpClient(); //DEV Only - interceptor for Stetho. // TODO remove from release version automatically client.networkInterceptors().add(new StethoInterceptor()); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); if (response != null) { forecastJsonStr = response.body().string(); } } catch (IOException e) { Log.e(LOG_TAG, "Error: ", e); e.printStackTrace(); } if (forecastJsonStr != null) { try { getWeatherDataFromJson(forecastJsonStr, locationQuery); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } } return null; }
From source file:com.example.retrofit.GitHubClient.java
License:Apache License
public static List<Contributor> getContributors() { OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(new StethoInterceptor()); // Create a very simple REST adapter which points the GitHub API endpoint. RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).setClient(new OkClient(client)) .build();// w w w . j a va 2 s . co m // Create an instance of our GitHub API interface. GitHub github = restAdapter.create(GitHub.class); // Fetch and print a list of the contributors to this library. return github.contributors("square", "retrofit"); }
From source file:com.example.test.myapplication.TestActivity.java
License:Apache License
private void initDatas() { new Thread(new Runnable() { @Override//from w w w . j a v a2 s.c o m public void run() { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("https://www.baidu.com/index.php").build(); Call response = okHttpClient.newCall(request); response.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Log.i("Response", e.toString()); } @Override public void onResponse(Response response) throws IOException { Log.i("Response", response.body().toString()); mDatas.add(response.toString()); runOnUiThread(new Runnable() { @Override public void run() { mAdapter.notifyDataSetChanged(); } }); } }); } }).start(); }
From source file:com.example.weatherforecast.service.HttpConnection.java
License:Open Source License
public OkHttpClient getHttpClient() { if (httpClient == null) { httpClient = new OkHttpClient(); } return httpClient; }
From source file:com.example.xyzreader.remote.HttpClientProvider.java
License:Apache License
private HttpClientProvider(Context context) { httpClient = new OkHttpClient(); File httpCacheDir = new File(context.getCacheDir(), "http_cache"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB httpClient.setCache(new Cache(httpCacheDir, httpCacheSize)); httpClient.setReadTimeout(10, TimeUnit.SECONDS); httpClient.setConnectTimeout(5, TimeUnit.SECONDS); }
From source file:com.examples.abelanav2.grpcclient.CloudStorage.java
License:Open Source License
/** * Uploads an image to Google Cloud Storage. * @param url the upload url.//from w w w . j a v a 2 s . c om * @param bitmap the image to upload. * @throws IOException if cannot upload the image. */ public static void uploadImage(String url, Bitmap bitmap) throws IOException { ByteArrayOutputStream bOS = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bOS); byte[] bitmapData = bOS.toByteArray(); InputStream stream = new ByteArrayInputStream(bitmapData); String contentType = URLConnection.guessContentTypeFromStream(stream); InputStreamContent content = new InputStreamContent(contentType, stream); MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg"); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBodyUtil.create(MEDIA_TYPE_JPEG, content.getInputStream()); Request request = new Request.Builder().url(url).put(requestBody).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } }
From source file:com.external.volley.toolbox.HurlStack.java
License:Apache License
/** * Opens an {@link HttpURLConnection} with parameters. * @param url/* w ww. j ava 2 s . co m*/ * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); HttpURLConnection connection = new OkUrlFactory(okHttpClient).open(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); connection.setRequestProperty("Charset", "UTF-8"); // ? connection.setRequestProperty("connection", "keep-alive"); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:com.f2prateek.couchpotato.data.DataModule.java
License:Apache License
static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); // Install an HTTP cache in the application cache directory. try {/* ww w. ja v a2 s. com*/ File cacheDir = new File(app.getCacheDir(), "http"); HttpResponseCache cache = new HttpResponseCache(cacheDir, DISK_CACHE_SIZE); client.setResponseCache(cache); } catch (IOException e) { Ln.e(e, "Unable to install disk cache."); } return client; }