文件上傳
http content-type
volley的整體實現(xiàn)流程款违,我就不寫了诸老,哎伍纫,看了一個上午主體還是沒什么問題宗雇,關鍵在于實現(xiàn)細節(jié),包含的東西才多莹规,所以這里不做過多討論赔蒲,言歸正傳,我們看看volley的一些細節(jié)實現(xiàn)吧。
乍一看舞虱,我們找遍了整個框架源碼欢际,都沒發(fā)現(xiàn)文件上傳寫好的API,并且你會驚訝的發(fā)現(xiàn)貌似request 實現(xiàn)中只有針對于content-type:application/x-www-form-urlencoded;我去感情這個框架沒寫好矾兜,其實呢损趋?我們先看看下面這段代碼.
/** Returns the content type of the POST or PUT body. */
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
}
/**
* Returns the raw POST or PUT body to be sent.
*
* <p>By default, the body consists of the request parameters in
* application/x-www-form-urlencoded format. When overriding this method, consider overriding
* {@link #getBodyContentType()} as well to match the new body format.
*
* @throws AuthFailureError in the event of auth failure
*/
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
/** Converts <code>params</code> into an application/x-www-form-urlencoded encoded string. */
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
看到了吧,哎椅寺,照這么說其它的實現(xiàn)浑槽,我們需要自己實現(xiàn),文件上傳了返帕,于是乎我們依葫蘆畫瓢桐玻,如果要實現(xiàn)文件上傳,我們需要改變請求頭content-type和body所以具體實現(xiàn)可參照如下荆萤,這里只貼關鍵代碼
具體參考Android volley 解析(三)之文件上傳篇
public String getBodyContentType() {
return "multipart/form-data;charset=utf-8;boundary=" + BOUNDARY;
}
public byte[] getBody(){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StringBuffer sb = new StringBuffer();
sb.append("--" + BOUNDARY);
sb.append("\r\n");
//解釋下name 這個你可以默認成key 是服務器解析的key镊靴,務必和服務器保持一致.
// filename 服務器保存的這個文件的名稱.
sb.append("Content-Disposition:form-data;name=\"file1\";filename=\"test.jpg\"\r\n");
sb.append("content-type:image/jpg\r\n\r\n");
bos.write(sb.toString().getBytes("utf-8"));
bos.write(...圖片數(shù)據(jù)byte[]...);
bos.write("\r\n".getBytes("utf-8"));
bos.write("--" + BOUNDARY + "--" + "\r\n".getBytes("utf-8"));
return bos.toByteArray();
}
以上我們必須實現(xiàn)Request 這個類,并且重寫getBody()和getBodyContentType()方法
volley緩存實現(xiàn).
1 HTTP 緩存
如果大家完全沒有緩存這個概念观腊,我推薦大家看這篇
從http開始說Volley緩存
http 緩存原理
表示緩存的請求或者響應頭報文
- Cache_Control
- 在請求頭里面If-None-Match / Etag
- If-Modified-Since /Last-Modified.
- Expires 和 Max-Age,date
-
響應狀態(tài)嗎304和200.
請求頭
響應頭
//這是StringRequest 的代碼,解析網(wǎng)絡結果.
@Override
@SuppressWarnings("DefaultCharset")
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
long now = System.currentTimeMillis();
Map<String, String> headers = response.headers;
long serverDate = 0;
long lastModified = 0;
long serverExpires = 0;
long softExpire = 0;
long finalExpire = 0;
long maxAge = 0;
long staleWhileRevalidate = 0;
boolean hasCacheControl = false;
boolean mustRevalidate = false;
String serverEtag = null;
String headerValue;
//RFC格式.這個表示服務器發(fā)送這條消息的時間邑闲。
headerValue = headers.get("Date");
if (headerValue != null) {
serverDate = parseDateAsEpoch(headerValue);
}
//這個的值有很多種.以下就是private,public,no-cache和no-store,max-age.
headerValue = headers.get("Cache-Control");
if (headerValue != null) {
hasCacheControl = true;
String[] tokens = headerValue.split(",", 0);
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.equals("no-cache") || token.equals("no-store")) {
return null;
} else if (token.startsWith("max-age=")) {
try {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
//這個被稱為死緩,總緩存時間是 maxAge + staleWhileRevalidate.
} else if (token.startsWith("stale-while-revalidate=")) {
try {
staleWhileRevalidate = Long.parseLong(token.substring(23));
} catch (Exception e) {
}
//如果有這個說明must-revalidate.不會使用死緩梧油,過了maxAge期限苫耸,則立即請求網(wǎng)絡.
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
mustRevalidate = true;
}
}
}
//響應頭中緩存的有效時間截止到如Date: Thu, 11 Jul 2015 15:33:24 GMT。是這種RFC格式.
headerValue = headers.get("Expires");
if (headerValue != null) {
//轉換成毫秒.
serverExpires = parseDateAsEpoch(headerValue);
}
//這個時間也是RFC格式.
headerValue = headers.get("Last-Modified");
if (headerValue != null) {
lastModified = parseDateAsEpoch(headerValue);
}
serverEtag = headers.get("ETag");
// Cache-Control takes precedence over an Expires header, even if both exist and Expires
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
finalExpire = mustRevalidate ? softExpire : softExpire + staleWhileRevalidate * 1000;
//expires(1.0) 和Cache-Control(1.1)那些過去
} else if (serverDate > 0 && serverExpires >= serverDate) {
// 緩存消息
softExpire = now + (serverExpires - serverDate);
finalExpire = softExpire;
}
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = finalExpire;
entry.serverDate = serverDate;
entry.lastModified = lastModified;
entry.responseHeaders = headers;
entry.allResponseHeaders = response.allHeaders;
return entry;
}
2 DiskBasedCache 內存和磁盤緩存
public class DiskBasedCache implements Cache {
/** 磁盤緩存儡陨,默認5M. */
private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024;
//這個位置是內存緩存.LinkedHash 符合LRU 的規(guī)則.
private final Map<String, CacheHeader> mEntries = new LinkedHashMap<>(16, .75f, true);
put(){};
get(){};
//這個函數(shù)是說 如果剩余空間(超過了期望的最大緩存)不足褪子,就會刪除文件.刪除文件規(guī)則,顯示跟LRU算法有關.
pruneIfNeeded(int needSpace);
}
總結
有些東西寫的不夠完善后期有時間都會慢慢補上骗村,請廣大高手們手下留情啊嫌褪,歡迎提出問題,一起討論.
為什么volley 適合數(shù)據(jù)量小的頻繁請求胚股?
volley中為了提高請求處理的速度笼痛,采用了ByteArrayPool進行內存中的數(shù)據(jù)存儲的,如果下載大量的數(shù)據(jù)琅拌,這個存儲空間就會溢出缨伊,所以不適合大量的數(shù)據(jù),但是由于他的這個存儲空間是內存中分配的进宝,當存儲的時候優(yōu)是從ByteArrayPool中取出一塊已經(jīng)分配的內存區(qū)域, 不必每次存數(shù)據(jù)都要進行內存分配刻坊,而是先查找緩沖池中有無適合的內存區(qū)域,如果有党晋,直接拿來用谭胚,從而減少內存分配的次數(shù) 徐块,所以他比較適合大量的數(shù)據(jù)量少的網(wǎng)絡數(shù)據(jù)交互情況灾而。