標籤:android volley 圖片載入
小弟很懶,不喜歡寫這些總結,主要是表達能力有限,如果想一起交流volley相關的,可以看看我的github
Volley
大家一起學習進步
1. Volley中添加url參數和表單 請求中添加Url參數,需要自己拼接字串,或者重寫getUrl方法
StringBuilder sb = new StringBuilder(); if (!url.contains("?")) { url = url + "?"; } else { if (!url.endsWith("?")) { url = url + "&"; } } Iterator<String> iterotor = paramsMap.keySet().iterator(); while (iterotor.hasNext()) { String key = (String) iterotor.next(); try { sb.append(key).append("=").append(URLEncoder.encode(paramsMap.get(key), "utf-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (sb.lastIndexOf("&") == sb.length() - 1) { sb.deleteCharAt(sb.length() - 1); }
post中添加參數 重寫Requst的getParams方法,或者自訂request
@Override protected Map<String, String> getParams() throws AuthFailureError { ParamsEngine engine = new ParamsEngine(mContext); Map<String, String> p = engine.generateRequestParams(paramsMap); return p; }
2. Volley載入本地圖片 Volley本身只能處理網路請求,緩衝網路請求。但是其request和response的架構同樣適用於非同步載入本地和本地檔案。所有網路請求的擷取,全部交給了BasicNetwork.它是Volley的核心,在performRequest這個方法中,將所有request轉換為相應的response,交給NetworkDispatcher來調度。我們只需要對request的url進行判斷。如果是/或者file開頭的,通通作為本地檔案處理,使用本地解析流的方式放回response,同時把request中的shouldCahce設為false,因為本地檔案已經存在本地,不再需要緩衝到volley的快取檔案中,代碼如下:
String urlString = request.getUrl(); if(urlString.trim().toLowerCase().startsWith("file")){ try { // 如果載入的是本地圖片,不放入disk-cache目錄中 request.setShouldCache(false); return new NetworkResponse(getFromFile(new URI(urlString).getPath())); } catch (URISyntaxException e) { e.printStackTrace(); } }else if(urlString.trim().toLowerCase().startsWith("/")){ request.setShouldCache(false); return new NetworkResponse(getFromFile(urlString)); }
其中getFromFile方法,就是把file轉換成位元組數組,可以參照volley中轉換entity的方法
private byte[] getFromFile(String urlString) throws IOException { File file = new File(urlString); PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) file.length()); byte[] buffer = null; // 得到檔案的輸入資料流 InputStream in = null; try { buffer = mPool.getBuf(1024); in = new FileInputStream(new File(urlString)); int count; while ((count = in.read(buffer)) != -1) { bytes.write(buffer, 0, count); } return bytes.toByteArray(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } mPool.returnBuf(buffer); try { bytes.close(); } catch (IOException e) { e.printStackTrace(); } } }
這裡使用ByteArrayPool來避免反覆申請byte[]數組空間
3. Volley中DiskBasedCache初始化非常慢的問題。 initialize方法中,遍曆所有的快取檔案,讀取頭資訊存入map中,當快取檔案非常多的時候,google提供的初始化實現,耗費了10s以上的時間!!!不知道這是不是個bug。也就是說,每次newRequestQueue,都需要等待十秒的時間才能開始處理request。如果這樣的話,這個架構就沒法用了。 目前我也沒找到好的方法。有兩個最佳化方案。 (1) 有人在stackoverflow中提到過這個問題,然後他給出瞭解決方案。在initialize中,給FileOutputStream在套一層BufferedOutputStream。本人測試過,速度有十倍的提升。基本能控制初始化的時間在1-3s。 (2) 提前初始化。每一次newRequestQueue都要花費這麼長時間初始化,但是實際使用中,我們需要new那麼多RequestQueue嗎?看原始碼可以知道,每個RequestQueue實際啟動了5個迴圈線程來調度各種請求。其中四個用來處理網路請求,一個用來處理cache請求。對於一個app來說,這樣已經足夠,我們沒必要每個頁面都啟動一個RequestQueue。因此,我把RequestQueue設定為單例,在Application中初始化,這樣就可以避免initialize被反覆調用,同時也能在介面出來之前,預先準備好資料
Android的volley架構心得