zoukankan      html  css  js  c++  java
  • Volley——网络请求httpstack(二)

      在之前的一篇博文中,我简略记录了,Volley的请求队列和线程管理的实现。这一次来记录一下HttpStack的工作过程

      

        /**
         * 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是如何工作的。

     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的实现也不算短,所以我们依然分段来阅读。

        @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中。

      接下来,我们来看看其中一些方法的实现。

      openConnection

        /**
         * 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的读使能。

      setConnectionParametersForRequest:

      

        @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");设置相应的方法类型。

      如果像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

  • 相关阅读:
    vpp编写plugin
    vrf 命令
    vxlan + 多个vrf
    dpdk helloworld
    Go函数高级
    Go_defer
    Go递归函数
    Go作用域
    Go函数
    Go字符串
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/5472352.html
Copyright © 2011-2022 走看看