Exemple #1
0
 static void init(Context context) {
   mRequestQueue = Volley.newRequestQueue(context);
   int memClass =
       ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
   // Use 1/8th of the available memory for this memory cache.
   int cacheSize = 1024 * 1024 * memClass / 8;
   // int cacheSize = memClass / 10;
   Logger.d(TAG, "MyVolley..cacheSize=" + cacheSize + "..memClass=" + memClass);
   mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize));
 }
  /**
   * @brief 加载网络文件函数。
   * @author joychang
   * @param[in] url XML文件网络地址。
   * @return XML文件数据流。
   */
  protected InputStream _load(String url) {
    Log.d(TAG, "_load() start");

    InputStream input = null;

    if (url != null) {
      try {
        URL connectUrl = new URL(url);
        if (connectUrl != null) {
          Logger.d("joychang", "isaiqiyi==" + isaiqiyi);
          // input = connectUrl.openStream();
          if (isaiqiyi) {
            input = connectUrl.openStream();
          } else {
            conn = (HttpURLConnection) connectUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10 * 1000);
            // input = conn.getInputStream();
            conn.setRequestProperty(
                "User-Agent",
                " Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko)");
            if (302 == conn.getResponseCode()) {
              String uri = conn.getHeaderField("location");
              URL mUrl = new URL(uri);
              conn = (HttpURLConnection) mUrl.openConnection();
              conn.setRequestMethod("GET");
              conn.setConnectTimeout(10 * 1000);
              //                        	HttpURLConnection mconn = (HttpURLConnection)
              // mUrl.openConnection();
              //                        	mconn.setRequestMethod("GET");
              //                        	mconn.setConnectTimeout(10 * 1000);
              input = conn.getInputStream();
            } else {
              input = conn.getInputStream();
            }
          }
          //                    //input = connectUrl.openStream();
        }
      } catch (MalformedURLException e) {
        e.printStackTrace();
        input = null;
      } catch (IOException e) {
        e.printStackTrace();
        input = null;
      }
    }

    Log.d(TAG, "_load() end");

    return input;
  }