okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)

没想到因为想要记录问题注册

也没想到的第一篇就是记录问题…………

最近新起一个项目,第一次封装网络请求……硬着头皮写

public class OkhttpProvider {

private static OkhttpProviderprovider;

private OkHttpClientclient;

private OkhttpProvider(){

OkHttpClient.Builder builder =new OkHttpClient.Builder()

.connectTimeout(ConstantUtils.CONNECT_TIMEOUT, TimeUnit.SECONDS)

.writeTimeout(ConstantUtils.WRITE_TIMEOUT, TimeUnit.SECONDS)

.readTimeout(ConstantUtils.READ_TIMEOUT, TimeUnit.SECONDS)

.addNetworkInterceptor(new CacheControlInterceptor())

.addInterceptor(new LoggingInterceptor())

.addInterceptor(new HeaderInterceptor());

//设置缓存

        File httpCacheDirectory =new File(

CustomApplication.getContext().getCacheDir(),"OkHttpCache");

builder.cache(new Cache(httpCacheDirectory,100 *1024 *1024));

client= builder.build();

}

public static OkhttpProvider getInstance(){

if (provider ==null){

provider =new OkhttpProvider();

}

return provider;

}

public OkHttpClient getClient() {

return client;

}

private static class CacheControlInterceptorimplements Interceptor{

@Override

        public Response intercept(Chain chain)throws IOException {

Request request = chain.request();

if (!NetworkUtils.isConnected(CustomApplication.getContext())) {

request = request.newBuilder()

.cacheControl(CacheControl.FORCE_CACHE)

.build();

Log.d("CacheControlInterceptor","no Network");

}

Response response = chain.proceed(request);

if (NetworkUtils.isConnected(CustomApplication.getContext())) {

int maxAge =60 *60;

String cacheControl = request.cacheControl().toString();

if (TextUtils.isEmpty(cacheControl)) {

cacheControl ="public, max-age=" + maxAge;

}

response = response.newBuilder()

.removeHeader("Pragma")

.header("Cache-Control", cacheControl)

.build();

}else {

int maxStale =60 *60 *24 *30;

response = response.newBuilder()

.removeHeader("Pragma")

.header("Cache-Control","public, only-if-cached, max-stale=" + maxStale)

.build();

}

return response;

}

}

private static class HeaderInterceptorimplements Interceptor {

@Override

        public Response intercept(Chain chain)throws IOException {

Request request = chain.request().newBuilder()

//                    .addHeader("","")

                    .build();

return chain.proceed(request);

}

}

private static class LoggingInterceptorimplements Interceptor {

@Override

        public Response intercept(Chain chain)throws IOException {

Request request = chain.request();

long t1 = System.nanoTime();

Log.i("okhttp", String.format(Locale.CHINA,"Sending request %s on %s%n%s",

request.url(), chain.connection(), request.headers()));

Response response = chain.proceed(request);

ResponseBody responseBody = response.peekBody(1024 *1024);

long t2 = System.nanoTime();

Log.i("okhttp", String.format(Locale.CHINA,"Received response for %s [%s] in %.1fms%n%s",

response.request().url(), responseBody.string(), (t2 - t1) /1e6d, response.headers()));

return response;

}

}

}

测试发现不管有没有网,取得都是缓存数据……

后来发现拦截器有问题,改成了这样

private OkhttpProvider(CacheControlInterceptor cacheInterceptor){

OkHttpClient.Builder builder =new OkHttpClient.Builder()

.connectTimeout(ConstantUtils.CONNECT_TIMEOUT, TimeUnit.SECONDS)

.writeTimeout(ConstantUtils.WRITE_TIMEOUT, TimeUnit.SECONDS)

.readTimeout(ConstantUtils.READ_TIMEOUT, TimeUnit.SECONDS)

.addInterceptor(cacheInterceptor)

.addNetworkInterceptor(cacheInterceptor)

.addInterceptor(new LoggingInterceptor())

.addInterceptor(new HeaderInterceptor());

//设置缓存

    File httpCacheDirectory =new File(

CustomApplication.getContext().getCacheDir(),"OkHttpCache");

builder.cache(new Cache(httpCacheDirectory,100 *1024 *1024));

client= builder.build();

}

public static OkhttpProvider getInstance(){

if (provider ==null){

provider =new OkhttpProvider(new CacheControlInterceptor());

}

return provider;

}

然后开始报错…………

08:56:49.878 32533-32533/com.neusoft.meda W/System.err: at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)

        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)

        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)

        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)

        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

05-07 08:56:49.879 32533-32533/com.neusoft.meda W/System.err:    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

        at com.neusoft.meda.rx.retrofit.OkhttpProvider$HeaderInterceptor.intercept(OkhttpProvider.java:97)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

        at com.neusoft.meda.rx.retrofit.OkhttpProvider$LoggingInterceptor.intercept(OkhttpProvider.java:110)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

        at com.neusoft.meda.rx.retrofit.OkhttpProvider$CacheControlInterceptor.intercept(OkhttpProvider.java:66)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

在网上差了一圈都没有结果,然后发现了一篇文章,虽然跟我的问题不一样,但是给了我提示啊!!!

我!没导入logging-interceptor包!!!

记录自己白痴的第一次网络封装……//哭

你可能感兴趣的:(okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160))