Пример #1
0
  private static RequestQueue newRequestQueue(Context context) {
    Proxy proxy = NetUtil.getProxy();
    File cacheDir =
        new File(DirectoryManager.getDirectory(DirectoryManager.DIR.CACHE), DEFAULT_CACHE_DIR);

    String userAgent = "CGTong/0";
    try {
      String packageName = context.getPackageName();
      PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
      userAgent = packageName + "/" + info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
      return null;
    }
    if (Build.VERSION.SDK_INT >= 9) {
      stack = new HurlStack(proxy);
    } else {
      // Prior to Gingerbread, HttpUrlConnection was unreliable.
      // See:
      // http://android-developers.blogspot.com/2011/09/androids-http-clients.html
      AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent);
      if (proxy != null) {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
      }
      stack = new HttpClientStack(client);
    }
    Network network = new BasicNetwork(stack);

    final DiskBasedCache diskCache = new DiskBasedCache(cacheDir);
    // 监听目录的变化,切换cache目录
    DirectoryManager.addSdCardListener(
        new DirectoryManager.SdcardStatusListener() {
          @Override
          public void onChange(SDCARD_STATUS status) {
            new Thread(
                    new Runnable() {
                      @Override
                      public void run() {
                        diskCache.switchCache(
                            new File(
                                DirectoryManager.getDirectory(DirectoryManager.DIR.CACHE),
                                DEFAULT_CACHE_DIR));
                      }
                    })
                .start();
          }
        });

    RequestQueue queue = new RequestQueue(diskCache, network);
    // RequestQueue queue = Volley.newRequestQueue(context);
    queue.start();
    return queue;
  }
Пример #2
0
 public static <T> T getCacheFile(InputBase input, Class<T> clazz) {
   Cache cache = requestQueue.getCache();
   Request<T> request =
       new GsonRequest<T>(input.method(), input.toString(), "", clazz, null, null, null, null);
   Cache.Entry entry = cache.get(request.getCacheKey());
   if (entry != null) {
     try {
       String json = new String(entry.data, "UTF-8");
       if (clazz == String.class) {
         return (T) json;
       }
       // File类型写文件后返回file对象
       else if (clazz == File.class) {
         String fileName = TextUtil.md5(input.toString());
         File outFile =
             new File(DirectoryManager.getDirectory(DirectoryManager.DIR.DATA), fileName);
         FileUtils.writeFile(outFile.getAbsolutePath(), entry.data);
         return (T) outFile;
       }
       // 替换服务端可能返回的错误的data格式
       json = json.replace("\"data\":[]", "\"data\":{}");
       // 服务器返回errNo=0
       if (json.matches(ERROR_NO_0_EXPRESSION)) {
         T data;
         JSONObject jsonObject = new JSONObject(json);
         json = jsonObject.getString("data");
         // 其他属于Gson请求,解析对象返回
         Gson gson = GsonUtil.createBuilder();
         data = (T) gson.fromJson(json, clazz);
         return data;
       }
       // 服务器返回errNo != 0
       else {
         return null;
       }
     } catch (Exception e) {
       return null;
     }
   }
   return null;
 }