本文整理汇总了Java中com.squareup.okhttp.Interceptor类的典型用法代码示例。如果您正苦于以下问题:Java Interceptor类的具体用法?Java Interceptor怎么用?Java Interceptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Interceptor类属于com.squareup.okhttp包,在下文中一共展示了Interceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newVineyardService
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public static AndroidTvBoilerplateService newVineyardService() {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// Catch unauthorised error
return response;
}
});
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.interceptors().add(interceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AndroidTvBoilerplateService.ENDPOINT)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(AndroidTvBoilerplateService.class);
}
示例2: addProgressResponseListener
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
/**
* 包装OkHttpClient,用于下载文件的回调
* @param client 待包装的OkHttpClient
* @param progressListener 进度回调接口
* @return 包装后的OkHttpClient,使用clone方法返回
*/
public static OkHttpClient addProgressResponseListener(OkHttpClient client, final ProgressResponseListener 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;
}
示例3: intercept
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.e("Log",
String.format("Sending request %s on %s%n%s", request.url(), chain.connection(),
request.headers()).toString());
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.e("Log",
String.format("Received response for %s in %.1fms%n%s", response.request().url(),
(t2 - t1) / 1e6d, response.headers()).toString());
return response;
}
示例4: getRestAdapter
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
/**
* Initializes the adapter and returns it
* The REST Adapter is created with Retrofit methods defined in MainApiClient
* Injects authorization headers into request
* Inspects status codes from response
* @return rest adapter
*/
private MainApiClient getRestAdapter() {
httpClient = new OkHttpClient();
httpClient.networkInterceptors().add(new StethoInterceptor());
httpClient.interceptors().add(new Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
com.squareup.okhttp.Response response = chain.proceed(request);
statusCode = response.code();
return response;
}
});
mRestAdapter = new RestAdapter.Builder()
.setEndpoint(URL_UAT)
.setClient(new OkClient(httpClient))
.setRequestInterceptor(authHeader())
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
return mRestAdapter.create(MainApiClient.class);
}
示例5: Client
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public Client() {
httpClient = new OkHttpClient();
httpClient.networkInterceptors().add(new Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
com.squareup.okhttp.Response response = chain.proceed(request);
IpTag tag = (IpTag) request.tag();
String ip = chain.connection().getSocket().getRemoteSocketAddress().toString();
tag.ip = ip;
return response;
}
});
httpClient.setConnectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
httpClient.setReadTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
}
示例6: addProgressResponseListener
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public static OkHttpClient addProgressResponseListener(OkHttpClient client, final IDownloadCallBack listener) {
// 克隆
OkHttpClient clone = client.clone();
clone.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(Chain arg0) throws IOException {
// 拦截
Response originalResponse = arg0.proceed(arg0.request());
return originalResponse
.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(),listener))
.build();
}
});
return clone;
}
示例7: addProgressResponseListener
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
/**
* 包装OkHttpClient,用于下载文件的回调
* @param client 待包装的OkHttpClient
* @param progressListener 进度回调接口
* @return 包装后的OkHttpClient,使用clone方法返回
*/
public static OkHttpClient addProgressResponseListener(OkHttpClient client,final ProgressResponseListener 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;
}
示例8: MovieApiDataSource
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public MovieApiDataSource() {
// Define the interceptor, add authentication headers
Interceptor interceptor = new Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder().addHeader("User-Agent", "Retrofit-Sample-App").build();
return chain.proceed(newRequest);
}
};
// Add the interceptor to OkHttpClient
OkHttpClient client = new OkHttpClient();
client.interceptors().add(interceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MOVIE_API_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService = retrofit.create(MovieApiEndpointInterface.class);
}
示例9: newVineyardService
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public static VineyardService newVineyardService() {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// Catch unauthorised error
return response;
}
});
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.interceptors().add(interceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(VineyardService.ENDPOINT)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(VineyardService.class);
}
示例10: intercept
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.d("Retrofit",String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.d("Retrofit", String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
示例11: intercept
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
System.out.println(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
System.out.println(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
示例12: LoginViewModel
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public LoginViewModel() {
List<Interceptor> interceptors = new ArrayList<>();
interceptors.add(new StethoInterceptor());
interceptors.add(chain -> {
Response response = chain.proceed(chain.request());
List<String> cookieHeaders = response.headers("set-cookie");
for (String header : cookieHeaders) {
if (header.contains("user")) {
mUserCookie = header.split(";")[0];
}
else if(header.contains("__cfduid")){
mCfduid = header.split(";")[0];
}
}
return response;
});
DaggerNetworkServiceComponent.builder()
.okClientModule(new OkClientModule(interceptors))
.appModule(HackerNewsApplication.getAppModule())
.appComponent(HackerNewsApplication.getAppComponent())
.build()
.inject(this);
}
示例13: initPicasso
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
private void initPicasso() {
File cacheDirectory = new File(getCacheDir().getAbsolutePath(), "OKHttpCache");
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setCache(new Cache(cacheDirectory, Integer.MAX_VALUE));
/** Dangerous interceptor that rewrites the server's cache-control header. */
okHttpClient.networkInterceptors().add(new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=432000")
.header("Pragma", "")
.build();
}
});
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(okHttpDownloader);
Picasso picasso = builder.build();
//picasso.setIndicatorsEnabled(true);
//picasso.setLoggingEnabled(true);
Picasso.setSingletonInstance(picasso);
}
示例14: verifyLoggingEnabled
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
private static void verifyLoggingEnabled(OkHttpClient client, List list) {
verify(client).interceptors();
ArgumentCaptor<Interceptor> interceptorCaptor = ArgumentCaptor.forClass(Interceptor.class);
verify(list).add(interceptorCaptor.capture());
assertThat(interceptorCaptor.getValue(), is(notNullValue()));
assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(), is(HttpLoggingInterceptor.Level.BODY));
}
示例15: proceed
import com.squareup.okhttp.Interceptor; //导入依赖的package包/类
public final Response proceed(Request paramRequest)
throws IOException
{
this.calls = (1 + this.calls);
if (this.index > 0)
{
Interceptor localInterceptor2 = (Interceptor)HttpEngine.this.client.networkInterceptors.get(-1 + this.index);
Address localAddress = HttpEngine.this.connection.route.address;
if ((!paramRequest.url().getHost().equals(localAddress.uriHost)) || (Util.getEffectivePort(paramRequest.url()) != localAddress.uriPort)) {
throw new IllegalStateException("network interceptor " + localInterceptor2 + " must retain the same host and port");
}
if (this.calls > 1) {
throw new IllegalStateException("network interceptor " + localInterceptor2 + " must call proceed() exactly once");
}
}
Response localResponse;
if (this.index < HttpEngine.this.client.networkInterceptors.size())
{
NetworkInterceptorChain localNetworkInterceptorChain = new NetworkInterceptorChain(HttpEngine.this, 1 + this.index, paramRequest);
Interceptor localInterceptor1 = (Interceptor)HttpEngine.this.client.networkInterceptors.get(this.index);
localResponse = localInterceptor1.intercept(localNetworkInterceptorChain);
if (localNetworkInterceptorChain.calls != 1) {
throw new IllegalStateException("network interceptor " + localInterceptor1 + " must call proceed() exactly once");
}
}
else
{
HttpEngine.this.transport.writeRequestHeaders(paramRequest);
if ((HttpEngine.this.permitsRequestBody()) && (paramRequest.body != null))
{
BufferedSink localBufferedSink = Okio.buffer(HttpEngine.this.transport.createRequestBody(paramRequest, paramRequest.body.contentLength()));
paramRequest.body.writeTo(localBufferedSink);
localBufferedSink.close();
}
localResponse = HttpEngine.this.readNetworkResponse();
}
return localResponse;
}