List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.burhan.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);/*from w w w . j av a 2s .c o m*/ client.setWriteTimeout(10, SECONDS); // Install an HTTP cache in the application cache directory. File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); return client; }
From source file:com.calgen.udacity.RetrofitTest.java
License:Apache License
@Test public void testGetJokes() throws IOException { //Building the RestAdapter again because UrlFetchClient needs appengine module // and doesn't work during testing. Also, OkClient is not supported by GCE RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(ServiceGenerator.API_ENDPOINT_URL) .setClient(new OkClient(new OkHttpClient())).build(); ApiInterface apiInterface = restAdapter.create(ApiInterface.class); assertEquals("Incorrect response : ", 5, apiInterface.getRandomJokesSync(5).getJokeList().size()); }
From source file:com.camel.crawler.WebCrawler.java
License:Open Source License
public WebCrawler() { client = new OkHttpClient(); emailPattern = Pattern.compile(regex); emailFile = new File("emailout.txt"); }
From source file:com.camnter.easygank.gank.EasyGank.java
License:Open Source License
private EasyGank() { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setReadTimeout(7676, TimeUnit.MILLISECONDS); /*/*from ww w . j a va 2 s . co m*/ * ?? */ if (EasyApplication.getInstance().log) { okHttpClient.interceptors().add(chain -> { Response response = chain.proceed(chain.request()); com.orhanobut.logger.Logger.d(chain.request().urlString()); return response; }); } Retrofit retrofit = new Retrofit.Builder().baseUrl(GankApi.BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create(EasyApplication.getInstance().gson)) .client(okHttpClient).build(); this.gankService = retrofit.create(GankService.class); }
From source file:com.capstone.transit.trans_it.TripPlannerActivity.java
License:Open Source License
private void getDirections(LatLng origin, LatLng destination) { final String directionsURL = getDirectionsUrl(origin, destination); Log.v(TAG, directionsURL);/*from w ww . j a va 2s . c o m*/ if (isNetworkAvailable()) { Thread T = new Thread() { public void run() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(directionsURL).build(); Call call = client.newCall(request); try { Response response = call.execute(); String jsonData = response.body().string(); Log.v(TAG, jsonData); if (response.isSuccessful()) { mStep = getSteps(jsonData); } else { alertUserAboutError(); } } catch (JSONException | IOException e) { e.printStackTrace(); } } }; T.start(); try { T.join(); } catch (InterruptedException e) { e.printStackTrace(); } } else { Toast.makeText(this, "unavailable network", Toast.LENGTH_LONG).show(); } }
From source file:com.carlospinan.demoretrofit2.helpers.API.java
License:Open Source License
public APIService getRetrofitService() { if (service == null) { OkHttpClient client = new OkHttpClient(); client.interceptors().add(new LoggingInterceptor()); Retrofit retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL) .addConverterFactory(GsonConverterFactory.create()).client(client).build(); service = retrofit.create(APIService.class); }//from w ww .jav a 2 s . c o m return service; }
From source file:com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory.java
License:Open Source License
public OkHttpClientHttpUrlConnectionFactory() { client = new OkHttpClient(); client.setConnectionSpecs(Arrays.asList(new ConnectionSpec[] { ConnectionSpec.CLEARTEXT, // for http new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions() .allEnabledCipherSuites().build() // for https }));/*from w w w . j av a2s. co m*/ factory = new OkUrlFactory(client); }
From source file:com.codemodlabs.coordinate.Token.java
License:Open Source License
private void executeCallback(String url) { class CallbackTask extends AsyncTask { @Override/*from ww w . ja v a 2 s.c o m*/ protected Object doInBackground(Object[] objects) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url((String) objects[0]).build(); Response response = null; String body_string = ""; try { response = client.newCall(request).execute(); body_string = response.body().string(); } catch (IOException e) { e.printStackTrace(); } Gson gson = new Gson(); callback = gson.fromJson(body_string, Callback.class); return callback; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); try { getTokenFromServer((Callback) result); } catch (IOException e) { e.printStackTrace(); } } } new CallbackTask().execute(url); }
From source file:com.codemodlabs.coordinate.Token.java
License:Open Source License
private void getTokenFromServer(Callback callback) throws IOException { class GetTokenTask extends AsyncTask { @Override//from w w w . ja va 2 s.c o m protected Object doInBackground(Object[] objects) { try { final String access_code = ((Callback) objects[0]).access_code; String url_string = "https://login.uber.com/oauth/token"; OkHttpClient client = new OkHttpClient(); // Ignore invalid SSL endpoints. client.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); RequestBody formBody = new FormEncodingBuilder().add("client_secret", client_secret) .add("client_id", client_id).add("grant_type", "authorization_code") .add("redirect_uri", redirect_url).add("code", access_code).build(); Request request = new Request.Builder().url(url_string).post(formBody).build(); Response response = client.newCall(request).execute(); String response_body = response.body().string(); Gson gson = new Gson(); token = gson.fromJson(response_body, Token.class); return token; } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); Token token = (Token) result; Intent resultIntent = new Intent(); resultIntent.putExtra("access_token", token.access_token); resultIntent.putExtra("expires_in", token.expires_in); resultIntent.putExtra("token_type", token.token_type); resultIntent.putExtra("refresh_token", token.refresh_token); resultIntent.putExtra("scope", token.scope); setResult(2, resultIntent); finish(); } } new GetTokenTask().execute(callback); }
From source file:com.comcast.freeflow.examples.artbook.models.DribbbleFetch.java
License:Apache License
public void load(final ArtbookActivity caller, int itemsPerPage, int page) { new AsyncTask<String, Void, String>() { OkHttpClient client = new OkHttpClient(); private Exception exception; protected String doInBackground(String... urls) { try { return get(new URL(urls[0])); } catch (Exception e) { this.exception = e; Log.e(TAG, "Exception: " + e); return null; }// ww w .ja va 2 s . co m } protected void onPostExecute(String data) { DribbbleFeed feed = new Gson().fromJson(data, DribbbleFeed.class); caller.onDataLoaded(feed); } String get(URL url) throws IOException { HttpURLConnection connection = client.open(url); InputStream in = null; try { in = connection.getInputStream(); byte[] response = readFully(in); return new String(response, "UTF-8"); } finally { if (in != null) in.close(); } } byte[] readFully(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; for (int count; (count = in.read(buffer)) != -1;) { out.write(buffer, 0, count); } return out.toByteArray(); } }.execute("http://api.dribbble.com/shots/popular?per_page=" + itemsPerPage + "&page=" + page); }