Volly 发送网络请求过程分析

来源:互联网 发布:landesk 软件中心 编辑:程序博客网 时间:2024/05/20 06:27

volly的发送和接受请求实现类为HurlStack


实际发送请求的为HttpURLConnection

在此函数中执行@Override

    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)

            throws IOException, AuthFailureError { }

。。。

HttpURLConnection connection = openConnection(parsedUrl, request);

。。。

for (String headerName : map.keySet()) {

            connection.addRequestProperty(headerName, map.get(headerName));


        }


设置http请求的header


setConnectionParametersForRequest(connection, request);

在此函数中设置http请求的content-type以及设置http body

默认request的构造函数为  Method.DEPRECATED_GET_OR_POST

public Request(String url, Response.ErrorListener listener) {

        this(Method.DEPRECATED_GET_OR_POSTurllistener);


    }



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;

volly中如果没有进行修改     返回表单content-type

public String getBodyContentType() {

        return"application/x-www-form-urlencoded; charset=" + getParamsEncoding();


    }




post  或者  get的请求处理处理为

private staticvoid 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();

        }


    }




在request类中

abstract protected Response<T>   parseNetworkResponse(NetworkResponseresponse);

为必须要实现的抽象方法


此方法在CacheDispatcher 和  NetworkDispatcher方法中的run方法中被调用


// We have a cache hit; parse its data for delivery back to the request.

                request.addMarker("cache-hit");

                Response<?> response =request.parseNetworkResponse(

                        new NetworkResponse(entry.data,entry.responseHeaders));


                request.addMarker("cache-hit-parsed”);


在NetworkDispatcher中,

NetworkResponse networkResponse =mNetwork.performRequest(request);

。。。

NetworkResponse networkResponse =mNetwork.performRequest(request);   在HurlStack中被定义,执行具体的网络请求。




/**

     * Subclasses must implement this to perform delivery of the parsed

     * response to their listeners.  The given response is guaranteed to

     * be non-null; responses that fail to parse are not delivered.

     * @param response The parsed response returned by

     * {@link #parseNetworkResponse(NetworkResponse)}

     */

    abstractprotectedvoid deliverResponse(Tresponse);


 if (mResponse.isSuccess()) {

                mRequest.deliverResponse(mResponse.result);

            } else {

                mRequest.deliverError(mResponse.error);


            }

调用回调

在此回调中mResponse  mRequest都为final变量


0 0
原创粉丝点击