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();
}
}
}原文地址:http://blog.csdn.net/android_love/article/details/42873881