標籤:android style blog http color 使用
上一篇介紹了Volley的使用,主要接觸了Request與RequestQueue這兩個類,這篇就來瞭解一下這兩個類的具體實現。
Request類圖:
Request類: Request是一個抽象類別,其中的主要屬性:
mMethod: 要求方法,目前支援GET, POST, PUT, DELETE, HEAD, OPTIONS,TRACE, PATCH方法
mUrl: 請求Url
mErrorListener: 錯誤處理監聽器,請求出錯時調用
mSequence: 請求的序號,相同優先順序的請求在請求隊列中根據序號來進行排序,序號低的排在隊列前面。
mRequestQueue: 該請求所在的請求隊列
mCacheEntry: When a request can be retrieved from cache but must be refreshed from the network, the cache entry will be stored here so that in the event of a "Not Modified" response, we can be sure it hasn‘t been evicted from cache.
mRetryPolicy: 請求的重試策略
mShouldCache: 該請求的響應是否被緩衝
mCanceled: 該請求是否能被取消
mResponseDelivered: 請求的響應是否已經交付。
另外還有一個mTag屬性,用來標記Request,可以在RequestQueue中根據tag來批量取消Request。
主要方法包括:
getBody(): 返回請求體的位元組數組表示。預設實現為返回null,所以如果是POST或PUT請求,子類需要重寫這個方法。
parseNetworkResponse() 與deliverResponse()為抽象方法,由子類實現。parseNetworkResponse用來解析原始的響應資訊,並返回一個特定的響應類型即Response<T>中的T類型結果。deliverResponse用來將解析好的響應結果交付給監聽器進行處理。
預設情況下,Request的Priority都是Normal,如下源碼:
1 /** 2 * Priority values. Requests will be processed from higher priorities to 3 * lower priorities, in FIFO order. 4 */ 5 public enum Priority { 6 LOW, 7 NORMAL, 8 HIGH, 9 IMMEDIATE10 }11 12 /**13 * Returns the {@link Priority} of this request; {@link Priority#NORMAL} by default.14 */15 public Priority getPriority() {16 return Priority.NORMAL;17 }
Request實現Comparable介面來對Request的優先順序進行比較,從而決定Request在隊列中的順序。優先順序越高,在請求隊列中排得越前,相同優先順序的序號越低,排得越前。
1 /** 2 * Our comparator sorts from high to low priority, and secondarily by 3 * sequence number to provide FIFO ordering. 4 */ 5 @Override 6 public int compareTo(Request<T> other) { 7 Priority left = this.getPriority(); 8 Priority right = other.getPriority(); 9 10 // High-priority requests are "lesser" so they are sorted to the front.11 // Equal priorities are sorted by sequence number to provide FIFO ordering.12 return left == right ?13 this.mSequence - other.mSequence :14 right.ordinal() - left.ordinal();15 }
Request派生出三個子類JsonRequest、ImageRequest、ClearCacheRequest。其中
1. JsonRequest<T>也是一個抽象類別,可以發送一個Json表示的請求體,並返回一個T類型的響應,主要包括
mListener: 請求成功的監聽器
mRequestBody:請求體的Json字串表示。
parseNetworkResponse()仍為抽象方法,getBody()返回mRequestBody的位元組數組。deliverResponse中調用mListener的onResponse方法,該方法由使用者自己定義。參考上篇。
1 @Override2 protected void deliverResponse(T response) {3 mListener.onResponse(response);4 }
JsonArrayRequest、JsonObjectRequest繼承自JsonRequest,分別表示返回一個JsonArray響應的請求與返回一個JsonObject響應的請求。JsonArrayRequest的parseNetworkResponse實現,JsonObjectRequest與之類似,不同的是返回JSONObject類型的響應,而不是JSONArray
1 @Override 2 protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { 3 try { 4 String jsonString = 5 new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 6 return Response.success(new JSONArray(jsonString), 7 HttpHeaderParser.parseCacheHeaders(response)); 8 } catch (UnsupportedEncodingException e) { 9 return Response.error(new ParseError(e));10 } catch (JSONException je) {11 return Response.error(new ParseError(je));12 }13 }
2. ImageRequest 用來根據一個URL來請求一個位元影像Bitmap,包括屬性
mListene:用來接收經過解碼的位元影像的監聽器
mMaxWidth: 解碼位元影像的最大寬度,
mMaxHeight:解碼位元影像的最大高度
如果mMaxWidth,mMaxHeight都為0,則保持位元影像的原始大小,如果其中一個不為0,則按照原始位元影像的寬高比進行解碼,如果都不為0, 則將解碼成最適合width x height地區並且保持原始位元影像寬高比的位元影像。
ImageRequest的優先順序是最低的。
1 @Override2 public Priority getPriority() {3 return Priority.LOW;4 }
3. ClearCacheRequest 一個類比的用來清理緩衝的請求
mCache:需要清理的緩衝
mCallback:緩衝清理完後在主線程中被調用的回調介面。
parseNetworkResponse與deliverResponse都是空實現,因為這是一個類比的Request,沒有實際的網路請求。
ClearCacheRequest的優先順序是最高的。
1 @Override2 public Priority getPriority() {3 return Priority.IMMEDIATE;4 }
不早了,今天先到這。明天繼續。