Volly 发送网络请求过程分析

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)}

     */

    abstractprotected void deliverResponse(Tresponse);


 if (mResponse.isSuccess()) {

                mRequest.deliverResponse(mResponse.result);

            } else {

                mRequest.deliverError(mResponse.error);


            }

调用回调

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值