Volley——網絡請求httpstack(二)

  在以前的一篇博文中,我簡略記錄了,Volley的請求隊列和線程管理的實現。這一次來記錄一下HttpStack的工做過程html

  

    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @param stack An {@link HttpStack} to use for the network, or null for default.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }

        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork(stack);

        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        queue.start();

        return queue;
    }

這段代碼在上一篇中貼過,是新建一個請求隊列的大體流程。咱們能夠看到,當SDK版本大於9時,就會使用HurlStack做爲HttpStack。下面咱們來看看,HurlStack是如何工做的。android

 1     public HurlStack() {
 2         this(null);
 3     }
 4 
 5     /**
 6      * @param urlRewriter Rewriter to use for request URLs
 7      */
 8     public HurlStack(UrlRewriter urlRewriter) {
 9         this(urlRewriter, null);
10     }
11 
12     /**
13      * @param urlRewriter Rewriter to use for request URLs
14      * @param sslSocketFactory SSL factory to use for HTTPS connections
15      */
16     public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) {
17         mUrlRewriter = urlRewriter;
18         mSslSocketFactory = sslSocketFactory;
19     }

  咱們發現,在建立HurlStack的代碼中,咱們參數所有傳入的null,即在默認條件下,mUrlRewriter和mSslSocketFactory都是爲空的。那麼這個初始化彷佛什麼都沒有作。其實,真正起的主要做用的是HurlStack中的performRequest方法。在BasicNetwork中,HurlStack只有performRequest會被調用。緩存

  performRequest的實現也不算短,因此咱們依然分段來閱讀。cookie

    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        String url = request.getUrl();
        HashMap<String, String> map = new HashMap<String, String>();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        if (mUrlRewriter != null) {
            String rewritten = mUrlRewriter.rewriteUrl(url);
            if (rewritten == null) {
                throw new IOException("URL blocked by rewriter: " + url);
            }
            url = rewritten;
        }
        URL parsedUrl = new URL(url);
        HttpURLConnection connection = openConnection(parsedUrl, request);
        for (String headerName : map.keySet()) {
            connection.addRequestProperty(headerName, map.get(headerName));
        }
        setConnectionParametersForRequest(connection, request);

 

  首先,從request中獲取url,和http的包頭map,咱們在發送http請求時,cookie這些東西就放在headers中,在新建時咱們已經讀過,在默認狀態下mUrlRewriter是爲null的,因此咱們能夠直接跳過if判斷。而後咱們創建HttpURLConnection,並將咱們的請求包頭一一設置到connection的RequstProperty中。ide

  接下來,咱們來看看其中一些方法的實現。post

  openConnectionui

    /**
     * Opens an {@link HttpURLConnection} with parameters.
     * @param url
     * @return an open connection
     * @throws IOException
     */
    private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
        HttpURLConnection connection = createConnection(url);

        int timeoutMs = request.getTimeoutMs();
        connection.setConnectTimeout(timeoutMs);
        connection.setReadTimeout(timeoutMs);
        connection.setUseCaches(false);
        connection.setDoInput(true);

        // use caller-provided custom SslSocketFactory, if any, for HTTPS
        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
        }

        return connection;
    }

 

   設置超時,設置緩存使能,設置InputStream的讀使能。this

  setConnectionParametersForRequest:url

  

    @SuppressWarnings("deprecation")
    /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection,
            Request<?> request) throws IOException, AuthFailureError {
        switch (request.getMethod()) {
            case Method.DEPRECATED_GET_OR_POST:
                // This is the deprecated way that needs to be handled for backwards compatibility.
                // If the request's post body is null, then the assumption is that the request is
                // GET.  Otherwise, it is assumed that the request is a POST.
                byte[] postBody = request.getPostBody();
                if (postBody != null) {
                    // Prepare output. There is no need to set Content-Length explicitly,
                    // since this is handled by HttpURLConnection using the size of the prepared
                    // output stream.
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.addRequestProperty(HEADER_CONTENT_TYPE,
                            request.getPostBodyContentType());
                    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                    out.write(postBody);
                    out.close();
                }
                break;
            case Method.GET:
                // Not necessary to set the request method because connection defaults to GET but
                // being explicit here.
                connection.setRequestMethod("GET");
                break;
            case Method.DELETE:
                connection.setRequestMethod("DELETE");
                break;
            case Method.POST:
                connection.setRequestMethod("POST");
                addBodyIfExists(connection, request);
                break;
            case Method.PUT:
                connection.setRequestMethod("PUT");
                addBodyIfExists(connection, request);
                break;
            case Method.HEAD:
                connection.setRequestMethod("HEAD");
                break;
            case Method.OPTIONS:
                connection.setRequestMethod("OPTIONS");
                break;
            case Method.TRACE:
                connection.setRequestMethod("TRACE");
                break;
            case Method.PATCH:
                connection.setRequestMethod("PATCH");
                addBodyIfExists(connection, request);
                break;
            default:
                throw new IllegalStateException("Unknown method type.");
        }
    }

 

  代碼行數很多,但其實內容比較簡單,就是request.getMethod()獲取請求類型,而後connection.setRequestMethod("GET");設置相應的方法類型。spa

  若是像POST或PATCH這些類型的請求,則還須要加上Body信息。

  addBodyIfExists

    private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
            throws IOException, AuthFailureError {
        byte[] body = request.getBody();
        if (body != null) {
            connection.setDoOutput(true);
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(body);
            out.close();
        }
    }

 

  這個方法中,將post部分,寫入請求的body中,咱們在本身的post請求時,只須要重寫getParams方法,便可修改Body的params。

 

  到這裏,就完成了HttpURLConnection對象的各類設置。接下來,就是建立和設置HttpResponse:

  

 1         // Initialize HttpResponse with data from the HttpURLConnection.
 2         ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
 3         int responseCode = connection.getResponseCode();
 4         if (responseCode == -1) {
 5             // -1 is returned by getResponseCode() if the response code could not be retrieved.
 6             // Signal to the caller that something was wrong with the connection.
 7             throw new IOException("Could not retrieve response code from HttpUrlConnection.");
 8         }
 9         StatusLine responseStatus = new BasicStatusLine(protocolVersion,
10                 connection.getResponseCode(), connection.getResponseMessage());
11         BasicHttpResponse response = new BasicHttpResponse(responseStatus);
12         if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
13             response.setEntity(entityFromConnection(connection));
14         }
15         for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
16             if (header.getKey() != null) {
17                 Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
18                 response.addHeader(h);
19             }
20         }
21         return response;

  咱們仔細觀察能夠發現,這一段代碼主要是將connect中的各類信息提取出來,而後設置到response中。其實設置提取connection的Entity單獨拉出一個方法,咱們能夠看一看:

  

    /**
     * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
     * @param connection
     * @return an HttpEntity populated with data from <code>connection</code>.
     */
    private static HttpEntity entityFromConnection(HttpURLConnection connection) {
        BasicHttpEntity entity = new BasicHttpEntity();
        InputStream inputStream;
        try {
            inputStream = connection.getInputStream();
        } catch (IOException ioe) {
            inputStream = connection.getErrorStream();
        }
        entity.setContent(inputStream);
        entity.setContentLength(connection.getContentLength());
        entity.setContentEncoding(connection.getContentEncoding());
        entity.setContentType(connection.getContentType());
        return entity;
    }

 

  這裏讀取了connection的InputStream,這裏咱們能夠聯繫到前面的代碼,connection.setDoInput(true);這就是爲何在初始化時要設置這個使能。

 

  自此,HurlStack的基本實現,就閱讀完畢。在這個類中,Volley建立並設置了HttpURLConnection 和 HttpResponse。這些對象是爲了後面BasicNetWork作準備,我將會在下一篇中閱讀BasicNetWork。

 

  Done

相關文章
相關標籤/搜索