Android 网络篇章(一) 全面解析Volley源码 (--小老弟你终于来了--)

1. Volley的使用

   RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); //第一步
	StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }); //第二步
        queue.add(request); //第三步 

2. 分析第一步 创建一个RequestQueue

  • 按照Volley的使用方法,我们最先开始要初始化一个RequestQueue,由于新建一个RequestQueue非常消耗资源,开发的时候只需创建一次即可

  • newRequestQueue分析

     public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
            BasicNetwork network;
       	//自定义的网络栈
            if (stack == null) {
                if (Build.VERSION.SDK_INT >= 9) {
                  //如果API>=2.3 使用HttpUrlConnection的HurlStack 
                    network = new BasicNetwork(new HurlStack());
                } else {
                    String userAgent = "volley/0";
                    try {
                        String packageName = context.getPackageName();
                        PackageInfo info =
                                context.getPackageManager().getPackageInfo(packageName, /* flags= */ 0);
                        userAgent = packageName + "/" + info.versionCode;
                    } catch (NameNotFoundException e) {
                    }
    								  //API小于2.3时候使用httpclient
                    network =new BasicNetwork(new HttpClientStack(AndroidHttpClient.newInstance(userAgent)));
                }
            } else {
                network = new BasicNetwork(stack);
            }
    		//返回一个RequestQueue
            return newRequestQueue(context, network);
        }
    
     private static RequestQueue newRequestQueue(Context context, Network network) {
       	//创建缓存目录
            File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
       //创建线程池 
            RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
       //启动线程池
            queue.start();
            return queue;
        }
    
    public void start() {
            stop(); //停止所有的线程
            mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, 					mDelivery);
      		//开启缓存线程
            mCacheDispatcher.start();
    
            //创建网络缓存池最大量的线程,并且启动线程 mDispatchers.length=4
            for (int i = 0; i < mDispatchers.length; i++) {
                NetworkDispatcher networkDispatcher =
                        new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
                mDispatchers[i] = networkDispatcher;
                networkDispatcher.start();
            }
        }
    
      //停止网络和缓存的调度器
        public void stop() {
            if (mCacheDispatcher != null) {
                mCacheDispatcher.quit();
            }
            for (final NetworkDispatcher mDispatcher : mDispatchers) {
                if (mDispatcher != null) {
                    mDispatcher.quit();
                }
            }
        }
    

    到这里我们发现Volley创建一个RequestQueue会在后台默认创建5个线程其中1个缓存线程和4个网络调度线程

1. 缓存线程做了什么事情

//提供对请求队列执行缓存分类的线程
public class CacheDispatcher extends Thread {
  //缓存请求队列
  private final BlockingQueue<Request<?>> mCacheQueue;
  //网络请求队列
   private final BlockingQueue<Request<?>> mNetworkQueue;
  //读取的缓存
  private final Cache mCache;
  //用于发布响应
   private final ResponseDelivery mDelivery;
  //线程存活标志
   private volatile boolean mQuit = false;
 // 管理具有相同缓存键的等待请求和重复数据消除请求的列表
   private final WaitingRequestManager mWaitingRequestManager;
  ...
    //既然是一个线程,我们必须从run开始分析
   @Override
    public void run() {
        if (DEBUG) VolleyLog.v("start new dispatcher");
    	//提高线程的优先级
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        // 阻塞线程,用来初始化
        mCache.initialize();

        while (true) {
            try {
              //对缓存队列进行处理
                processRequest();
            } catch (InterruptedException e) {
                // 处理缓存队列时,出现异常,关闭线程
                if (mQuit) {
                    Thread.currentThread().interrupt();
                    return;
                }
                VolleyLog.e(
                        "Ignoring spurious interrupt of CacheDispatcher thread; "
                                + "use quit() to terminate it");
            }
        }
    }
 private void processRequest() throws InterruptedException {
        //从缓存分类队列获取请求,阻止直到//至少有一个可用
        final Request<?> request = mCacheQueue.take();
   			//缓存线程的执行过程//******//
        processRequest(request);
    }
  void processRequest(final Request<?> request) throws InterruptedException {
    	//给请求添加标记 主要是在日志中的输入.
        request.addMarker("cache-queue-take");
			//如果请求已经关闭,则添加完成标记 不让发送请求体
        if (request.isCanceled()) {
            request.finish("cache-discard-canceled");
            return;
        }
        // 得到缓存池中的缓存
        Cache.Entry entry = mCache.get(request.getCacheKey());
    	//如果没有缓存
        if (entry == null) {
            request.addMarker("cache-miss");
            //如果没有缓存,把当前的请求发送给网络调度线程,
            if (!mWaitingRequestManager.
                //maybeAddToWaitingRequests(request)) {
                mNetworkQueue.put(request);
            }
            return;
        }
					//判断缓存是否过期
        if (entry.isExpired()) {
            request.addMarker("cache-hit-expired");
          	//缓存过期后,把过期的缓存添加到请求体中
            request.setCacheEntry(entry);
          //把请求体发送给网络调度线程去处理
            if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
                mNetworkQueue.put(request);
            }
            return;
        }

        // 得到一个缓存.
        request.addMarker("cache-hit");
				//分析其数据以将其传递回请求
        Response<?> response =
                request.parseNetworkResponse(
                        new NetworkResponse(entry.data, entry.responseHeaders));
        request.addMarker("cache-hit-parsed");
        if (!entry.refreshNeeded()) {
            //未过期。只需给出响应
          	//响应添加到缓存
            mDelivery.postResponse(request, response);
        } else {
            //如果软缓存。我们可以传递缓存的响应,但是我们还需要将请求发送到网络进行刷新
            request.addMarker("cache-hit-refresh-needed");
            request.setCacheEntry(entry);
          	//响应体设置为刷新状态
            response.intermediate = true;
         		//如果当前请求没有发送给等待列表
            if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
                	//将响应体发给用户,并且将请求体转发给网络调度器重新请求数据
                mDelivery.postResponse(
                        request,
                        response,
                        new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    mNetworkQueue.put(request);
                                } catch (InterruptedException e) {
                                    // Restore the interrupted status
                                    Thread.currentThread().interrupt();
                                }
                            }
                        });
            } else {
             //将响应的结果存入缓存
              //后面会介绍这个方法
                mDelivery.postResponse(request, response);
            }
        }
    }
  //等待请求管理器 
  private static class WaitingRequestManager implements Request.NetworkRequestCompleteListener {
    //有重复请求的请求的临时区域
        private final Map<String, List<Request<?>>> mWaitingRequests = new HashMap<>();
        private final CacheDispatcher mCacheDispatcher;
        WaitingRequestManager(CacheDispatcher cacheDispatcher) {
            mCacheDispatcher = cacheDispatcher;
        }
      	//请求接收到一个有效响应,可供其他等待请求使用
        @Override
        public void onResponseReceived(Request<?> request, Response<?> response) {
          //缓存为null或者缓存过期
            if (response.cacheEntry == null || response.cacheEntry.isExpired()) {
              //未收到可用响应时
                onNoUsableResponseReceived(request);
                return;
            }
          //拿到请求体的缓存key
            String cacheKey = request.getCacheKey();
            List<Request<?>> waitingRequests;
            synchronized (this) {
              //处理当前请求体的缓存
                waitingRequests = mWaitingRequests.remove(cacheKey);
            }
            if (waitingRequests != null) {
                if (VolleyLog.DEBUG) {
                    VolleyLog.v(
                            "Releasing %d waiting requests for cacheKey=%s.",
                            waitingRequests.size(), cacheKey);
                }
                for (Request<?> waiting : waitingRequests) {
                  //处理所有排队的请求
                    mCacheDispatcher.mDelivery.postResponse(waiting, response);
                }
            }
        }
    			//接收到一个有效请求响应,其他等待请求可以使用该响应
        @Override
        public synchronized void onNoUsableResponseReceived(Request<?> request) {
            String cacheKey = request.getCacheKey();
          //移除当前请求的缓存
            List<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
            if (waitingRequests != null && !waitingRequests.isEmpty()) {
                if (VolleyLog.DEBUG) {
                    VolleyLog.v(
                            "%d waiting requests for cacheKey=%s; resend to network",
                            waitingRequests.size(), cacheKey);
                }
              	//拿到当前队列的第一条请求
                Request<?> nextInLine = waitingRequests.remove(0);
                mWaitingRequests.put(cacheKey, waitingRequests);
                nextInLine.setNetworkRequestCompleteListener(this);
                try {
                    mCacheDispatcher.mNetworkQueue.put(nextInLine);
                } catch (InterruptedException iex) {
                    VolleyLog.e("Couldn't add request to queue. %s", iex.toString());
                    Thread.currentThread().interrupt();
                    mCacheDispatcher.quit();
                }
            }
        }
   	 //对于可缓存请求,如果相同缓存键的请求已经在运行中,将其添加到队列中,等待该运行中请求完成
        private synchronized boolean maybeAddToWaitingRequests(Request<?> request) {
            String cacheKey = request.getCacheKey();
          //判断请求队列中是否存在相同的缓存
            if (mWaitingRequests.containsKey(cacheKey)) {
                List<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
                if (stagedRequests == null) {
                    stagedRequests = new ArrayList<>();
                }
                request.addMarker("waiting-for-response");
                stagedRequests.add(request);
                mWaitingRequests.put(cacheKey, stagedRequests);
                if (VolleyLog.DEBUG) {
                    VolleyLog.d("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
                }
                return true;
            } else {
              //添加占位的缓存
                mWaitingRequests.put(cacheKey, null);
                request.setNetworkRequestCompleteListener(this);
                if (VolleyLog.DEBUG) {
                    VolleyLog.d("new request, sending to network %s", cacheKey);
                }
                return false;
            }
        }
    } 
}

2. 网络调度线程的作用

public class NetworkDispatcher extends Thread {
   //缓存请求队列
  private final BlockingQueue<Request<?>> mCacheQueue;
  //网络请求队列
   private final BlockingQueue<Request<?>> mNetworkQueue;
  //读取的缓存
  private final Cache mCache;
  //用于发布响应
   private final ResponseDelivery mDelivery;
  //线程存活标志
   private volatile boolean mQuit = false;
  ....
    @Override
    public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        while (true) {
            try {
              //处理缓存队列
                processRequest();
            } catch (InterruptedException e) {
                // We may have been interrupted because it was time to quit.
                if (mQuit) {
                    Thread.currentThread().interrupt();
                    return;
                }
                VolleyLog.e(
                        "Ignoring spurious interrupt of NetworkDispatcher thread; "
                                + "use quit() to terminate it");
            }
        }
    }
    private void processRequest() throws InterruptedException {
        Request<?> request = mQueue.take();
        processRequest(request);
    }
  @VisibleForTesting
    void processRequest(Request<?> request) {
      //得到当前的时间
        long startTimeMs = SystemClock.elapsedRealtime();
        try {
            request.addMarker("network-queue-take");
          //请求已经取消
            if (request.isCanceled()) {
                request.finish("network-discard-cancelled");
              //通知网络监听器请求不可用
                request.notifyListenerResponseNotUsable();
                return;
            }
          //添加流量统计标记
            addTrafficStatsTag(request);
          //执行网络请求 
            NetworkResponse networkResponse = mNetwork.performRequest(request);
            request.addMarker("network-http-complete");
        	  // 如果服务器返回304,并且我们已经传递了一个响应,
             //我们完成了不要传递第二个相同的响应
            if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                request.finish("not-modified");
                request.notifyListenerResponseNotUsable();
                return;
            }

            //分析工作线程上的响应
            Response<?> response = request.parseNetworkResponse(networkResponse);
            request.addMarker("network-parse-complete");

            // 写入缓存
            // TODO: Only update cache metadata instead of entire record for 304s.
            if (request.shouldCache() && response.cacheEntry != null) {
                mCache.put(request.getCacheKey(), response.cacheEntry);
                request.addMarker("network-cache-written");
            }

            //将响应体返回
            request.markDelivered();
          	//将请求添加到缓存
            mDelivery.postResponse(request, response);
            request.notifyListenerResponseReceived(response);
        } catch (VolleyError volleyError) {
            volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
            parseAndDeliverNetworkError(request, volleyError);
            request.notifyListenerResponseNotUsable();
        } catch (Exception e) {
            VolleyLog.e(e, "Unhandled exception %s", e.toString());
            VolleyError volleyError = new VolleyError(e);
            volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
            mDelivery.postError(request, volleyError);
            request.notifyListenerResponseNotUsable();
        }
    }
}

3. 网络响应

public class BasicNetwork implements Network {
  ...
    @Override
    public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    //网络请求的时间
        long requestStart = SystemClock.elapsedRealtime();
    	//死循环
        while (true) {
            HttpResponse httpResponse = null;
            byte[] responseContents = null;
            List<Header> responseHeaders = Collections.emptyList();
            try {
                // Gather headers.
                Map<String, String> additionalRequestHeaders =
                        getCacheHeaders(request.getCacheEntry());
              	//返回一个网络请求的响应数据
                httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);
              	//得到响应码
                int statusCode = httpResponse.getStatusCode();
								//得到响应头
                responseHeaders = httpResponse.getHeaders();
                // 进行缓存验证
                if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
                    Entry entry = request.getCacheEntry();
                    if (entry == null) {
                        return new NetworkResponse(
                                HttpURLConnection.HTTP_NOT_MODIFIED,
                                /* data= */ null,
                                /* notModified= */ true,
                                SystemClock.elapsedRealtime() - requestStart,
                                responseHeaders);
                    }
                    // 合并缓存头和响应头 ,完成一次网络请求响应
                    List<Header> combinedHeaders = combineHeaders(responseHeaders, entry);
                    return new NetworkResponse(
                            HttpURLConnection.HTTP_NOT_MODIFIED,
                            entry.data,
                            /* notModified= */ true,
                            SystemClock.elapsedRealtime() - requestStart,
                            combinedHeaders);
                }

                //检查响应体是否完整(没有内容等..)
                InputStream inputStream = httpResponse.getContent();
                if (inputStream != null) {
                    responseContents =
                            inputStreamToBytes(inputStream, httpResponse.getContentLength());
                } else {
                   //无内容的请求
                    responseContents = new byte[0];
                }

                // 记录请求很慢的响应
                long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
                logSlowRequests(requestLifetime, request, responseContents, statusCode);
							//错误处理
                if (statusCode < 200 || statusCode > 299) {
                    throw new IOException();
                }
              //返回一个完整的网络响应
                return new NetworkResponse(
                        statusCode,
                        responseContents,
                        /* notModified= */ false,
                        SystemClock.elapsedRealtime() - requestStart,
                        responseHeaders);
            } 
          ...//异常处理
    }
  ...
}

4.将请求的内容放入缓存

mDelivery.postResponse(request, response);
//实现类
public class ExecutorDelivery implements ResponseDelivery {
  ...
  	 @Override
    public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
        request.markDelivered();
        request.addMarker("post-response");
    	//主线程发布一个响应 mResponsePoster为主线程创建的线程池
        mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
    }
  ...
      private class ResponseDeliveryRunnable implements Runnable {
         @SuppressWarnings("unchecked")
        @Override
        public void run() {
            // 请求关闭,
            if (mRequest.isCanceled()) {
                mRequest.finish("canceled-at-delivery");
                return;
            }
            //请求结果是否成功
            if (mResponse.isSuccess()) {
                mRequest.deliverResponse(mResponse.result);
            } else {
                mRequest.deliverError(mResponse.error);
            }
          	//中间的标记 ,
            if (mResponse.intermediate) {
                mRequest.addMarker("intermediate-response");
            } else {
                mRequest.finish("done");
            }
            if (mRunnable != null) {
                mRunnable.run();
            }
       }
      }
}

2.第二步 创建一个Request对象

请求结果

  • 找到Request的实现类 如stringRequest imageLoad
//成功
@Override
    protected void deliverResponse(String response) {
        Response.Listener<String> listener;
        synchronized (mLock) {
            listener = mListener;
        }
        if (listener != null) {
            listener.onResponse(response);
        }
    }
//错误 这个回调在Request中就处理了
 public void deliverError(VolleyError error) {
        Response.ErrorListener listener;
        synchronized (mLock) {
            listener = mErrorListener;
        }
        if (listener != null) {
            listener.onErrorResponse(error);
        }
    }

3. 将Request放入到RequestQueue中

 public <T> Request<T> add(Request<T> request) {
        // 标记这个请求体
        request.setRequestQueue(this);
        synchronized (mCurrentRequests) {
          //把请求体添加到请求集合中
            mCurrentRequests.add(request);
        }

        // 按照添加顺序来执行网络请求
        request.setSequence(getSequenceNumber());
        request.addMarker("add-to-queue");

        // 判断请求是否能缓存 不能缓存直接跳过缓存队列执行网络请求
        if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }
        mCacheQueue.add(request);
        return request;
    }

4. 流程图

Android 网络篇章(一) 全面解析Volley源码 (--小老弟你终于来了--)_第1张图片

Volley源码中涉及到的知识

1. Volley是如何把请求的数据回调回主线程中的?

//初始化RequestQueue中创建了一个Executor异步
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
        this(
                cache,
                network,
                threadPoolSize,
                new ExecutorDelivery(new Handler(Looper.getMainLooper())));
    } 
//当ExecutorDelivery.run执行完毕后回执行   handler.post(command);完成线程调度
public ExecutorDelivery(final Handler handler) {
        // Make an Executor that just wraps the handler.
        mResponsePoster = new Executor() {
            @Override
            public void execute(Runnable command) {
                handler.post(command);
            }
        };
    }

2. Volley开启了几个后台线程?

总共开启了5个线程:1个缓存调度线程和4个网络调度线程,并且线程的优先级为10,即后台线程。Volley其实并没有开启线程池去维护线程

3. Volley进行网络请求时用到了Http协议的哪些字段?

public class HttpHeaderParser {
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
  ...
    //Data 服务器返回的时间
headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = parseDateAsEpoch(headerValue);
        }
  		//缓存控制器
        headerValue = headers.get("Cache-Control");
        if (headerValue != null) {
            hasCacheControl = true;
            String[] tokens = headerValue.split(",");
            for (int i = 0; i < tokens.length; i++) {
                String token = tokens[i].trim();
							//不缓存响应数据,如果需要缓存响应数据,
                if (token.equals("no-cache") || token.equals("no-store")) {
                    return null;
                  //当需要设置缓存时,通过maxAge的值来设置缓存过期的时间。
                } else if (token.startsWith("max-age=")) {
                    try {
                        maxAge = Long.parseLong(token.substring(8));
                    } catch (Exception e) {
                    }
                } else if (token.startsWith("stale-while-revalidate=")) {
                    try {
                        staleWhileRevalidate = Long.parseLong(token.substring(23));
                    } catch (Exception e) {
                    }
                  //缓存数据过期前,可以使用缓存 如果过期必须强制对服务器进行校验
                } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                    mustRevalidate = true;
                }
            }
        }
  		//设置缓存过期的时间
        headerValue = headers.get("Expires");
        if (headerValue != null) {
            serverExpires = parseDateAsEpoch(headerValue);
        }
        headerValue = headers.get("Last-Modified");
        if (headerValue != null) {
            lastModified = parseDateAsEpoch(headerValue);
        }
        serverEtag = headers.get("ETag");

 }
}

用到的Http协议的字段如下:

  • Date:返回服务器时间
  • Cache-Control:为no-cache和no-store:不缓存响应数据,如果需要缓存响应数据,当需要设置缓存时,通过maxAge的值来设置缓存过期的时间。
  • Must-revalidate和proxy-revalidate:该值为一个boolean值,服务器告诉客户端,缓存数据过期前,可以使用缓存;缓存一旦过期,必须去源服务器进行有效性校验。
  • Expires:设置缓存过期的时间,如果 Cache-Control设置为需要缓存,那么优先以 Cache-Control的maxAge的值来设置缓存过期时间。
  • Last-Modified:在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是客户端请求的资源,同时有一个Last-Modified的属性标记此文件在服务器端最后被修改的时间。
    客户端第二次请求此URL时,根据HTTP协议的规定,浏览器会向服务器传送If-Modified-Since报头,询问该时间之后文件是否有被修改过,如果服务器端的资源没有变化,则自动返回 HTTP 304(Not Changed.)状态码,内容为空,这样就节省了传输数据量。它和请求头的if-modified-since字段去判断资源有没有被修改的。
  • ETags:它和if-None-Match(HTTP协议规格说明定义ETag为“被请求变量的实体值”,或者是一个可以与Web资源关联的记号)常用来判断当前请求资源是否改变。类似于Last-Modified和HTTP-IF-MODIFIED-SINCE。但是有所不同的是Last-Modified和HTTP-IF-MODIFIED-SINCE只判断资源的最后修改时间,而ETags和If-None-Match可以是资源任何的任何属性,不是资源的MD5等

你可能感兴趣的:(学习笔记)