Volley源码分析(一)

1.Volley源码分析(一)
2.Volley源码分析(二)
3.Volley源码分析(三)
4.XVolley-基于Volley的封装的工具类

第一次一行一行读源码,记录下来,慢慢来

1.Volley.class

用过Volley的都知道使用Volley的第一步需要使用Volley.newRequestQueue方法创建一个RequestQueue。所以就从这开始吧。
public class Volley {

/** Default on-disk cache directory. */
private static final String DEFAULT_CACHE_DIR = "volley";

/**
 * 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) {
            //大于2.3则建立HurlStack
            stack = new HurlStack();
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            //小于2.3版本则建立HttpClientStack
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    /**
     * 创建一个网络请求
     */
    Network network = new BasicNetwork(stack);

    /**
     * 这里每次都会创建一个请求队列
     */
    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}

/**
 * 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.
 * @return A started {@link RequestQueue} instance.
 */
public static RequestQueue newRequestQueue(Context context) {
    return newRequestQueue(context, null);
}

}
首先大体看,这里newRequestQueue方法使用了重载,提供两种方式创建RequestQueue。实际上最后执行的都是newRequestQueue(Context context, HttpStack stack)方法。详细看下这个方法。

第一步

建立缓存,可以看到这里利用文件流建立了缓存。

第二步

这里是个重点,当没有HttpStack == null时,这里对版本进行了判断,大于等于2.3则创建HurlStack,小于则创建HttpClientStack。两种的区别网上可以搜,大体就是2.3前HttpClientStack使用的HttpClient拥有较少的bug,2.3以后HttpURLConnection拥有更小的体积,更多的api,更多的优化。
tips:记得这里曾经有一次面试问到用到了什么设计模式,现在补一下,用到了策略模式
这里分析一下:
HttpStack.class

public interface HttpStack {
    /**
     * Performs an HTTP request with the given parameters.
     *
     * <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
     * and the Content-Type header is set to request.getPostBodyContentType().</p>
     *
     * @param request the request to perform
     * @param additionalHeaders additional headers to be sent together with
     *         {@link Request#getHeaders()}
     * @return the HTTP response
     */
    HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError;

}

可以看到HttpStack是一个接口,定义了performRequest方法,而HurlStack和HttpClientStack则实现了这个接口,分别实现了不同的实现方式。对应在不同的版本判断时调用将实现不同的类。这里提供下《HeadFrist 设计模式》读书笔记 —— 策略模式博客里面的类图
这里写图片描述
所有综上所述,这里用到的是策略模式。

第三步

创建了一个网络请求,这个后面会分析这两个类,现在只要知道这个类的作用是真正执行网络请求的地方。

第四步

创建了一个请求队列,并执行请求队列的start方法。这里可以看到每次都会创建一个请求队列其实不是特别合适的,这里可以考虑优化使用单例模式公用一个队列

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值