赞
踩
- public synchronized ExecutorService executorService() {
-
- if (executorService == null) {
-
- executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
-
- new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
-
- }
-
- return executorService;
-
- }
- //创建okHttpClient对象
- OkHttpClient mOkHttpClient = new OkHttpClient(); //第一步
- //创建一个Request
- final Request request = new Request.Builder()
- .url("https://github.com/hongyangAndroid")
- .build(); //第二步
- //new call
- Call call = mOkHttpClient.newCall(request); //第三步
- //请求加入调度
- call.enqueue(new Callback() //第四步
- {
- @Override
- public void onFailure(Request request, IOException e)
- {
- }
-
- @Override
- public void onResponse(final Response response) throws IOException
- {
- //String htmlStr = response.body().string();
- }
- });
- /**
- * Prepares the {@code request} to be executed at some point in the future.
- */
-
- @Override public Call newCall(Request request) {
-
- return new RealCall(this, request, false /* for web socket */);
-
- }
- @Override public void enqueue(Callback responseCallback) {
-
- synchronized (this) {
-
- if (executed) throw new IllegalStateException("Already Executed");
-
- executed = true;
-
- }
-
- captureCallStackTrace();
-
- client.dispatcher().enqueue(new AsyncCall(responseCallback)); //主要看这一步
-
- }
- synchronized void enqueue(AsyncCall call) {
- //如果正在运行的call小于设置的最大值,那么将该Runnable加入到运行列表并执行,否则加入到等待列表
- if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
-
- runningAsyncCalls.add(call);
-
- executorService().execute(call);
-
- } else {
-
- readyAsyncCalls.add(call);
-
- }
-
- }
- public abstract class NamedRunnable implements Runnable {
-
- protected final String name;
-
-
-
- public NamedRunnable(String format, Object... args) {
-
- this.name = Util.format(format, args);
-
- }
-
-
-
- @Override public final void run() {
-
- String oldName = Thread.currentThread().getName();
-
- Thread.currentThread().setName(name);
-
- try {
-
- execute();
-
- } finally {
-
- Thread.currentThread().setName(oldName);
-
- }
-
- }
-
-
-
- protected abstract void execute();
-
- }
- @Override protected void execute() {
-
- boolean signalledCallback = false;
-
- try {
-
- Response response = getResponseWithInterceptorChain();
-
- if (retryAndFollowUpInterceptor.isCanceled()) {
-
- signalledCallback = true;
- //通过回调就能得到结果
- responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
-
- } else {
-
- signalledCallback = true;
- //通过回调就能得到结果了,但是是在子线程
- responseCallback.onResponse(RealCall.this, response);
-
- }
-
- } catch (IOException e) {
-
- if (signalledCallback) {
-
- // Do not signal the callback twice!
-
- Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
-
- } else {
-
- responseCallback.onFailure(RealCall.this, e);
-
- }
-
- } finally {
-
- client.dispatcher().finished(this);
-
- }
-
- }
- Response getResponseWithInterceptorChain() throws IOException {
-
- // Build a full stack of interceptors.
-
- List<Interceptor> interceptors = new ArrayList<>();
-
- interceptors.addAll(client.interceptors());
-
- interceptors.add(retryAndFollowUpInterceptor);
-
- interceptors.add(new BridgeInterceptor(client.cookieJar()));
-
- interceptors.add(new CacheInterceptor(client.internalCache()));
-
- interceptors.add(new ConnectInterceptor(client));
-
- if (!forWebSocket) {
-
- interceptors.addAll(client.networkInterceptors());
-
- }
-
- interceptors.add(new CallServerInterceptor(forWebSocket));
-
-
-
- Interceptor.Chain chain = new RealInterceptorChain(
-
- interceptors, null, null, null, 0, originalRequest);
-
- return chain.proceed(originalRequest);
-
- }
- /**
- * A concrete interceptor chain that carries the entire interceptor chain: all application
- * interceptors, the OkHttp core, all network interceptors, and finally the network caller.
- */
-
- public final class RealInterceptorChain implements Interceptor.Chain {
-
- private final List<Interceptor> interceptors;
-
- private final StreamAllocation streamAllocation;
-
- private final HttpCodec httpCodec;
-
- private final Connection connection;
-
- private final int index;
-
- private final Request request;
-
- private int calls;
-
-
-
- public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
- HttpCodec httpCodec, Connection connection, int index, Request request) {
-
- this.interceptors = interceptors;
-
- this.connection = connection;
-
- this.streamAllocation = streamAllocation;
-
- this.httpCodec = httpCodec;
-
- this.index = index;
-
- this.request = request;
-
- }
-
-
-
- @Override public Connection connection() {
-
- return connection;
-
- }
-
-
-
- public StreamAllocation streamAllocation() {
-
- return streamAllocation;
-
- }
-
-
-
- public HttpCodec httpStream() {
-
- return httpCodec;
-
- }
-
-
-
- @Override public Request request() {
-
- return request;
-
- }
-
-
-
- @Override public Response proceed(Request request) throws IOException {
-
- return proceed(request, streamAllocation, httpCodec, connection);
-
- }
-
-
-
- public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
- Connection connection) throws IOException {
-
- if (index >= interceptors.size()) throw new AssertionError();
-
-
-
- calls++;
-
-
-
- // If we already have a stream, confirm that the incoming request will use it.
-
- if (this.httpCodec != null && !sameConnection(request.url())) {
-
- throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
-
- + " must retain the same host and port");
-
- }
-
-
-
- // If we already have a stream, confirm that this is the only call to chain.proceed().
-
- if (this.httpCodec != null && calls > 1) {
-
- throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
-
- + " must call proceed() exactly once");
-
- }
-
-
-
- // Call the next interceptor in the chain.
-
- RealInterceptorChain next = new RealInterceptorChain(
-
- interceptors, streamAllocation, httpCodec, connection, index + 1, request);
-
- Interceptor interceptor = interceptors.get(index);
-
- Response response = interceptor.intercept(next);
-
-
-
- // Confirm that the next interceptor made its required call to chain.proceed().
-
- if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
-
- throw new IllegalStateException("network interceptor " + interceptor
-
- + " must call proceed() exactly once");
-
- }
-
-
-
- // Confirm that the intercepted response isn't null.
-
- if (response == null) {
-
- throw new NullPointerException("interceptor " + interceptor + " returned null");
-
- }
-
-
-
- return response;
-
- }
-
-
-
- private boolean sameConnection(HttpUrl url) {
-
- return url.host().equals(connection.route().address().url().host())
-
- && url.port() == connection.route().address().url().port();
-
- }
-
- }
- public final class ConnectInterceptor implements Interceptor {
-
- public final OkHttpClient client;
-
-
-
- public ConnectInterceptor(OkHttpClient client) {
-
- this.client = client;
-
- }
-
-
-
- @Override public Response intercept(Chain chain) throws IOException {
-
- RealInterceptorChain realChain = (RealInterceptorChain) chain;
-
- Request request = realChain.request();
-
- StreamAllocation streamAllocation = realChain.streamAllocation();
-
-
-
- // We need the network to satisfy this request. Possibly for validating a conditional GET.
-
- boolean doExtensiveHealthChecks = !request.method().equals("GET");
-
- HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
-
- RealConnection connection = streamAllocation.connection();
-
-
-
- return realChain.proceed(request, streamAllocation, httpCodec, connection);
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。