Android的volley框架心得
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); }
@Override protected Map<String, String> getParams() throws AuthFailureError { ParamsEngine engine = new ParamsEngine(mContext); Map<String, String> p = engine.generateRequestParams(paramsMap); return p; }
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)); }
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(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。