RetryAndFollowUpInterceptor

这个拦截作用是:失败重连和重定向 直接上码 :

public Response intercept(Chain chain) throws IOException {
		Request request = chain.request();
		RealInterceptorChain realChain = (RealInterceptorChain) chain;
		Call call = realChain.call();
		EventListener eventListener = realChain.eventListener();
		// 创建网络资源如:HttpCodec Socket RealConnection或者释放 
		streamAllocation = new StreamAllocation(client.connectionPool(), createAddress
				(request.url()),
				call, eventListener, callStackTrace);

		int followUpCount = 0;//重连了几次
		Response priorResponse = null;
		while (true) {
			if (canceled) {
				streamAllocation.release(); //请求中取消 将抛出IO
				throw new IOException("Canceled");
			}

			Response response;
			boolean releaseConnection = true;
			try {
				response = realChain.proceed(request, streamAllocation, null, null);// 
				// 调用下一个拦截器
				releaseConnection = false;
			} catch (RouteException e) {
				// The attempt to connect via a route failed. The request will not have 
				// been sent.
				//recover 判断各种条件是否可以重连,true 继续,false throw 出异常
				if (!recover(e.getLastConnectException(), false, request)) {
					throw e.getLastConnectException();
				}
				releaseConnection = false;
				continue;
			} catch (IOException e) {

				//recover 判断是否各种条件是否可以重连,true 继续,false throw 出异常
				// An attempt to communicate with a server failed. The request may have 
				// been sent.
				boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
				if (!recover(e, requestSendStarted, request)) throw e;
				releaseConnection = false;
				continue;
			} finally { // We're throwing an unchecked exception. Release any resources. 
				if (releaseConnection) { //能执行到这里 说明 proceed和catch 都抛出了异常
					streamAllocation.streamFailed(null);
					streamAllocation.release();
				}
			}
			// Attach the prior response if it exists. Such responses never have a body. 
			if (priorResponse != null) { //能执行到这里说明 必然循环一次因为 在方法的后面才复制 
				response = response.newBuilder()
						.priorResponse(priorResponse.newBuilder()如果 响应体 body 为null 将抛异常
								.body(null).build())
						.build();
			}
			Request followUp = followUpRequest(response);// 通过 响应吗 去执行相应对的操作 
			if (followUp == null) {
				if (!forWebSocket) {
					streamAllocation.release();
				}
				return response; // good bye 
				}
				closeQuietly(response.body());
				if (++followUpCount > MAX_FOLLOW_UPS) {
					//重连次数超过时 将抛异常
					streamAllocation.release();
					throw new ProtocolException("Too many follow-up requests: " + 
							followUpCount);
				}
				if (followUp.body() instanceof UnrepeatableRequestBody) {
					streamAllocation.release();
					throw new HttpRetryException("Cannot retry streamed HTTP body", 
							response.code());
				}
				if (!sameConnection(response, followUp.url())) { //判断url和port是否相同 相同good
					// bye 不是重新分配资源
					streamAllocation.release();
					streamAllocation = new StreamAllocation(client.connectionPool(),
							createAddress(followUp.url()), call, eventListener, 
							callStackTrace);
				} else if (streamAllocation.codec() != null) {
					throw new IllegalStateException("Closing the body of " + response + 
							" didn't close its backing stream. Bad interceptor?");
				}
				request = followUp;
				priorResponse = response; ///这里才赋值 } 
			}



E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: com.example.read, PID: 22568 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:111) at java.lang.Thread.run(Thread.java:920) Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at okhttp3.internal.connection.RealRoutePlanner.planConnectToRoute$okhttp(RealRoutePlanner.kt:195) at okhttp3.internal.connection.RealRoutePlanner.planConnect(RealRoutePlanner.kt:152) at okhttp3.internal.connection.RealRoutePlanner.plan(RealRoutePlanner.kt:67) at okhttp3.internal.connection.FastFallbackExchangeFinder.launchTcpConnect(FastFallbackExchangeFinder.kt:118) at okhttp3.internal.connection.FastFallbackExchangeFinder.find(FastFallbackExchangeFinder.kt:62) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:267) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:158) at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:106) at java.lang.Thread.run(Thread.java:920) 怎么解决
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值