`
f303153041
  • 浏览: 44795 次
社区版块
存档分类
最新评论

Volley 核心源码解析(一)

阅读更多
Http 请求的基本过程

  volley 的 初始化 RequestQueue  requestQueue = Volley.newRequestQueue(context),

返回的是一个 RequestQueue  对象,这个对象的作用下面再说。现在进入到方法的内部可以看到

这么几个重载的方法:

  public static RequestQueue newRequestQueue(Context context) {
        return newRequestQueue(context, null);
    }

  public static RequestQueue newRequestQueue(Context context, HttpStack stack)
    {
    return newRequestQueue(context, stack, -1);
    }
  
public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
        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);



此时我们看看 HttpClientStack类,那么这个类是做什么的呢?

public class HttpClientStack implements HttpStack

HttpStack 是一个接口 仅仅只有一个方法:

public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError;

[align=left][/align]
参数是一个Request<?>的对象 这是一个泛型,它的作用下面再说。
那么这个方法译为中文就是:执行请求。

在HttpClientStack 中 performRequest的具体实现如下:

@Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        onPrepareRequest(httpRequest);
        HttpParams httpParams = httpRequest.getParams();
        int timeoutMs = request.getTimeoutMs();
        // TODO: Reevaluate this connection timeout based on more wide-scale
        // data collection and possibly different for wifi vs. 3G.
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
        return mClient.execute(httpRequest);
    }


protected final HttpClient mClient;

直到这时候,才发现Volley 默认使用的是apache Http Client框架,记得很早以前看到一篇文章说Android开发团队 推荐使用java.net 包中的HttpUrlConnection作为网络请求的方式,针对android 平台做过优化,却不知为何在Volley中 还是默认使用HttpClient?

再来看看Network 接口;

public NetworkResponse performRequest(Request<?> request) throws VolleyError;

仍然只有一个执行请求的方法。

在实现类BasicNetwork 的构造方法中,HttpStack 被作为了参数。

@Override
    public NetworkResponse performRequest(Request<?> request) throws VolleyError {
        long requestStart = SystemClock.elapsedRealtime();
        while (true) {
            HttpResponse httpResponse = null;
            byte[] responseContents = null;
            Map<String, String> responseHeaders = Collections.emptyMap();
            try {
                // Gather headers.
                Map<String, String> headers = new HashMap<String, String>();
                addCacheHeaders(headers, request.getCacheEntry());
                httpResponse = mHttpStack.performRequest(request, headers);
                StatusLine statusLine = httpResponse.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                  .....
}

看到这里 我们似乎明白了volley 对http请求的调用顺序是:

Network ----> HttpStack---->HttpClient


如果读者细心会发现,现有的网络执行过程中,不支持上传文件等属性,
在Volley 初始化 请求队列的时候,有一个重载方法:
public static RequestQueue newRequestQueue(Context context, HttpStack stack)
    {
    return newRequestQueue(context, stack, -1);
    }
   
那么要实现更复杂的网络请求, 只需要实现自己的HttpStack 在
performRequest() 方法中灵活实现即可满足需求。



Volley 的 Request对象


阅读源代码 我们发现
public abstract class Request<T> implements Comparable<Request<T>>

Request 是一个泛型的抽象类,且是可比较的。

那么为什么要实现 Comparable接口呢?

我的理解是要避免重复请求。


public interface Method {
        int DEPRECATED_GET_OR_POST = -1;
        int GET = 0;
        int POST = 1;
        int PUT = 2;
        int DELETE = 3;
        int HEAD = 4;
        int OPTIONS = 5;
        int TRACE = 6;
        int PATCH = 7;
    }


在Request中 有Method接口 定义了支持的Http请求方法。

abstract protected Response<T> parseNetworkResponse(NetworkResponse response);
这是一个抽象方法,那么要实现自定义的请求 就必须实现这个方法。如GsonRequest,ImageRequest等等。

发送Post请求必须重写getPostParams()方法:

@Deprecated
    protected Map<String, String> getPostParams() throws AuthFailureError {
        return getParams();
    }


至此,对Volley 的网络请求的过程以及源码的分析 告一段落。



  下一节 请求队列 RequestQueue  http://f303153041.iteye.com/blog/2281350
1
1
分享到:
评论

相关推荐

    Volley源码解析

    Volley源码解析,个人的理解与注释

    volley源码和jar包

    这是volley的源码以及jar包,用于小数据量的频繁的网络请求。

    Volley框架源码

    关于Volley框架的源码,及一些关于开发的一些例子讲解

    android-volley源码

    android-volley源码代码 android Volley ---------- This is an unofficial mirror for [android volley library](https://android.googlesource.com/platform/frameworks/volley), the source code will ...

    Volley源码+包

    Volley源码+包:在开发android使用第三方框架有两种方法:一是直接加框架的源码,二是在libs中加jar包。

    volley源码

    volley源码,有兴趣的同学可以下载研究一下。 谷歌建议使用volley框架进行网络请求。

    volley联网解析网络上的xml文件

    采用volley的联网方式,实现解析服务端返回的xml数据

    google官方最新volley源码

    google官方最新volley源码,如果你想对volley有更深的认识就下来学习吧

    Github-volley jar包和源码

    volley是一个优秀的安卓开源网络访问工具 这里包含一个volley代码jar和源码,版本是2015.03.03的1.0.11版本 更多资料可以参见volley的github地址: https://github.com/mcxiaoke/android-volley

    android volley源码

    android volley源码,从google下载的volley源码,方便理解和使用

    Volley源码与jar包

    Volley是2013年Google I/O上发布的,它是Android平台上的网络通信库,对常用的网络通信功能作了封装,能使网络通信更快, 更简单,更健壮。以前使用网络通信一般都是用AsyncTaskLoader、HttpURLConnection、...

    Volley源码

    Volley源码,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4255488.html

    Android Volley源码20160414版

    Android Volley源代码,更新日期20160414

    可查看Volley源码

    volley作为库文件 这个demo ,可以查看volley源码

    volley的demo(包括volley源码)

    一共两个demo: 1.使用volley.jar进行开发的demo 2.将volley源码添加到工程中,进行演示的demo。最重要的是在源码中添加了很多中文注释。

    Volley源码及文档

    Volley源码和文档,本人验证无误,遂上传,希望对你有帮助~

    volley框架源码实例

    volley框架的实例源码,包含二次封装,可下载导入工程项目运行

Global site tag (gtag.js) - Google Analytics