List of usage examples for com.squareup.okhttp OkHttpClient networkInterceptors
List networkInterceptors
To view the source code for com.squareup.okhttp OkHttpClient networkInterceptors.
Click Source Link
From source file:cn.com.crcement.oa.base.download.helper.ProgressHelper.java
License:Apache License
/** * OkHttpClient/*from w w w . ja v a 2 s. com*/ * * @param client * OkHttpClient * @param progressListener * ? * @return ?OkHttpClientclone */ public static OkHttpClient addProgressResponseListener(OkHttpClient client, final ProgressListener progressListener) { // OkHttpClient clone = client.clone(); // clone.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { // Response originalResponse = chain.proceed(chain.request()); // ? return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); } }); return clone; }
From source file:cn.edu.zafu.coreprogress.helper.ProgressHelper.java
License:Apache License
/** * OkHttpClient//from w w w . j av a 2s .co m * @param client OkHttpClient * @param progressListener ? * @return ?OkHttpClientclone */ public static OkHttpClient addProgressResponseListener(OkHttpClient client, final ProgressListener progressListener) { // OkHttpClient clone = client.clone(); // clone.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { // Response originalResponse = chain.proceed(chain.request()); //? return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); } }); return clone; }
From source file:com.brq.wallet.external.glidera.api.GlideraService.java
private GlideraService(@NonNull final NetworkParameters networkParameters) { Preconditions.checkNotNull(networkParameters); this.networkParameters = networkParameters; this.baseUrl = getBaseUrl(networkParameters); if (networkParameters.isTestnet()) { clientId = TESTNET_CLIENT_ID;/* www . j a v a 2s .c om*/ } else { clientId = MAINNET_CLIENT_ID; } /** * The Sha256 HMAC hash of the message. Use the secret matching the access_key to hash the message. * The message is the concatenation of the X-ACCESS-NONCE + URI of the request + message body JSON. * The final X-ACCESS-SIGNATURE is the HmacSha256 of the UTF-8 encoding of the message as a Hex encoded string */ final Interceptor apiCredentialInterceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (_oAuth1Response != null) { Request.Builder requestBuilder = request.newBuilder(); synchronized (_nonce) { final String nonce = _nonce.getNonceString(); final String uri = request.urlString(); String message = nonce + uri; if (request.body() != null && request.body().contentLength() > 0) { Buffer bodyBuffer = new Buffer(); request.body().writeTo(bodyBuffer); byte[] bodyBytes = bodyBuffer.readByteArray(); String body = new String(bodyBytes, Charsets.UTF_8); message += body; } final byte[] messageBytes = message.getBytes(Charsets.UTF_8); final byte[] secretBytes = _oAuth1Response.getSecret().getBytes(Charsets.UTF_8); final byte[] signatureBytes = Hmac.hmacSha256(secretBytes, messageBytes); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Hex.encode(signatureBytes, stream); final String signature = stream.toString(); request = requestBuilder.header(HEADER_ACCESS_KEY, _oAuth1Response.getAccess_key()) .header(HEADER_ACCESS_NONCE, nonce).header(HEADER_ACCESS_SIGNATURE, signature) .build(); } } return chain.proceed(request); } }; OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(15000, TimeUnit.MILLISECONDS); client.setReadTimeout(15000, TimeUnit.MILLISECONDS); client.networkInterceptors().add(apiCredentialInterceptor); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.); objectMapper.registerModule(new WapiJsonModule()); /* We should always add client_id to the header */ RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade requestFacade) { requestFacade.addHeader(HEADER_CLIENT_ID, clientId); } }; /* Create the json adapter */ RestAdapter adapter = new RestAdapter.Builder().setEndpoint(baseUrl + "/api/" + API_VERSION + "/") //.setLogLevel(RestAdapter.LogLevel.FULL) .setLogLevel(RestAdapter.LogLevel.BASIC).setLog(new AndroidLog("Glidera")) .setConverter(new JacksonConverter(objectMapper)).setClient(new NullBodyAwareOkClient(client)) .setRequestInterceptor(requestInterceptor).build(); glideraApi = adapter.create(GlideraApi.class); }
From source file:com.davidecirillo.dashboard.data.RestService.java
License:Apache License
public void create() { final OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); if (BuildConfig.DEBUG) loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); else/*from www . ja va 2s. c om*/ loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE); List<Interceptor> interceptors = new ArrayList<>(); interceptors.add(loggingInterceptor); interceptors.add(new LoggingInterceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request request = original.newBuilder().method(original.method(), original.body()).build(); return chain.proceed(request); } }); client.networkInterceptors().addAll(interceptors); retrofit.Retrofit restAdapterLocal = new retrofit.Retrofit.Builder().client(client) .baseUrl(AppConfig.http + mLocalUrl).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); retrofit.Retrofit restAdapterRemote = new retrofit.Retrofit.Builder().client(client) .baseUrl(AppConfig.http + mRemoteUrl).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); mLocalPiApi = restAdapterLocal.create(PIApi.class); mRemotePiApi = restAdapterRemote.create(PIApi.class); }
From source file:com.example.app.di.modules.ApiModule.java
License:Apache License
@Provides @Singleton//from w ww .ja v a 2s.com @Named("noCache") OkHttpClient provideNoCacheOkHttpClient(HttpLoggingInterceptor loggingInterceptor, @Named("userAgent") String userAgentValue) { OkHttpClient client = new OkHttpClient(); client.interceptors().add(loggingInterceptor); client.networkInterceptors().add(new UserAgentInterceptor(userAgentValue)); client.networkInterceptors().add(new StethoInterceptor()); return client; }
From source file:com.example.app.di.modules.ApiModule.java
License:Apache License
@Provides @Singleton/*from www .j a v a 2s. co 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 ww w. j a v a2s. 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.photo_frame.RestClientUtil.java
License:Apache License
public static RestClient getInstance(final Credentials credentials) { OkHttpClient client = OkHttpClientFactory.makeClient(); client.networkInterceptors().add(new StethoInterceptor()); return new RestClient(credentials, client); }
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();/*from ww w. ja 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"); }