• volley3--Volley类


    Volley这个类,Volley作为整个框架的入口,其实就是创建了一个RequestQueue队列

     1 public class Volley {  
     2   
     3     /**  
     4      * Default on-disk cache directory. 
     5      * 默认缓存目录名  
     6      */  
     7     private static final String DEFAULT_CACHE_DIR = "volley";  
     8   
     9     /** 
    10      * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it. 
    11      * You may set a maximum size of the disk cache in bytes. 
    12      * 创建一个默认工作池,并且启动请求队列 
    13      * @param context A {@link Context} to use for creating the cache dir.用于创建缓存目录 
    14      * @param stack An {@link HttpStack} to use for the network, or null for default. 
    15      * @param maxDiskCacheBytes the maximum size of the disk cache, in bytes. Use -1 for default size. 
    16      * 硬盘缓存最大值,-1则默认 
    17      * @return A started {@link RequestQueue} instance. 
    18      */  
    19     public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {  
    20         File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);//缓存队列  
    21   
    22         String userAgent = "volley/0";  
    23         try {  
    24             String packageName = context.getPackageName();//包名  
    25             PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);  
    26             userAgent = packageName + "/" + info.versionCode;  
    27         } catch (NameNotFoundException e) {  
    28         }  
    29   
    30         if (stack == null) {//如果没有限定stack  
    31             if (Build.VERSION.SDK_INT >= 9) {//adk版本在9或者以上  
    32                 stack = new HurlStack();  
    33             } else {  
    34                 // Prior to Gingerbread, HttpUrlConnection was unreliable.  
    35                 // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html  
    36                 stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));  
    37             }  
    38         }  
    39   
    40         Network network = new BasicNetwork(stack);  
    41           
    42         RequestQueue queue;  
    43         if (maxDiskCacheBytes <= -1)//小于等于-1,则默认值  
    44         {  
    45             // No maximum size specified  
    46             queue = new RequestQueue(new DiskBasedCache(cacheDir), network);  
    47         }  
    48         else  
    49         {  
    50             // Disk cache size specified  
    51             queue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network);  
    52         }  
    53   
    54         queue.start();  
    55   
    56         return queue;  
    57     }  

    可以看到,Volley的newRequestQueue()方法里面,根据版本创建了stack(分别是HurlStack和HttpClientStack)。至于不同adk版本会创建不同的stack,是由于android的版本原因,在9以上版本使用HurlStack比HttpClientStack更加好。

    然后创建了网络类BasicNetwork,缓存类DiskBasedCache,然后使用这两个来创建RequestQueue对象。

    或许现在大家还不明白这两个类的作用,大家只需要记得创建RequestQueue需要这两个类就可以了。

  • 相关阅读:
    Excel组件使用配置文档下载
    mysql 的书籍推荐~
    基于HTTP 协议认证介绍与实现
    DebianKvm快速安装上手教程
    spring事务,TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    项目中用到的设计模式(持续更新)
    项目中用到的架构模式(持续更新)
    添加验证
    ASP.NET Core中使用appsettings.json
    使用 ASP.NET Core 和 MongoDB 创建 Web API
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/7627130.html
Copyright © 2020-2023  润新知