Volley內(nèi)部使用的是HTTP形式
Volley優(yōu)點:
①網(wǎng)絡(luò)請求可以通過隊列排序
②網(wǎng)絡(luò)請求可以設(shè)置優(yōu)先級
③可以和Activity生命周期聯(lián)動
④非常適用于數(shù)據(jù)量不大但是通信頻繁的操作
⑤可以多級別取消請求
Volley缺點:
對于比較大的數(shù)據(jù),下載大文件等,不太適用
API:
請求String類型數(shù)據(jù):StringRequest
請求JSON數(shù)據(jù):JsonRequest:①JsonObjectRequest ? ? ②JsonArrayRequest
請求圖片數(shù)據(jù):ImageRequest?
核心步驟:
①創(chuàng)建網(wǎng)絡(luò)請求隊列:
RequestQueue queue=Volley.RequestQueue(context);
②創(chuàng)建請求(三種類型):
1.String類型的請求
StringRequest request= ?
new StringRequest(Method,url,Listener<String>,ErrorListener<String>)
第一個參數(shù):請求方式Method.GET,Method.POST等(若為空默認為GET請求)
第二個參數(shù):要請求的地址
第三個參數(shù):new Respone.Listener<String>并重寫onResponse(String)方法,該方法為請求成功的回調(diào)方法?(Listener的泛型要看具體為什么請求,String請求泛型為String,Json請求泛型為JsonObject或JsonArray,Image請求泛型返回Bitmap)
第四個參數(shù):new Respone.ErrorListener<String>并重寫Response.ErrorListener()方法,該方法為請求失敗的回調(diào)方法
2.JSON類型的請求:
JsonRequest jsonRequest=new JsonRequest(Method,url,jsonRequest,Listener<JsonObject>,ErrorListener<JsonObject>)
第一個參數(shù):請求方式Method.GET,Method.POST等(若為空默認為GET請求)
第二個參數(shù):要請求的地址
第三個參數(shù):JsonRequest,一般為null
第四個參數(shù):new Respone.Listener<Json>并重寫onResponse(JsonObject或JsonArray)方法,該方法為請求成功的回調(diào)方法(Listener的泛型要看具體為什么請求,String請求泛型為String,Json請求泛型為JsonObject或JsonArray,Image請求泛型返回Bitmap)
第五個參數(shù):new Respone.ErrorListener<>并重寫Response.ErrorListener<>()方法,該方法為請求失敗的回調(diào)方法
3.Image請求(ImageRequest,ImageLoader,NetworkImageView):
⑴ImageRequest(步驟與核心步驟一樣)
ImageRequest imageRequest=new ImageRequest(Method,url,Listener<>,maxWidth,maxHeight,decodeconfigErrorListener<>)
第一個參數(shù):請求方式Method.GET,Method.POST等(若為空默認為GET請求)
第二個參數(shù):要請求的地址
第三個參數(shù):new Respone.Listener<Bitmap>并重寫onResponse(Bitmap)方法,該方法為請求成功的回調(diào)方法(Listener的泛型要看具體為什么請求,String請求泛型為String,Json請求泛型為JsonObject或JsonArray,Image請求泛型返回Bitmap)
第四個參數(shù):maxWidth為設(shè)置下載的圖片最大的寬度,若為0則為不設(shè)置
第五個參數(shù):maxHeight為設(shè)置下載的圖片最大的高度,若為0則為不設(shè)置
第六個參數(shù):decodeConfig為圖片的質(zhì)量,config.ARGB(A為透明度,RGB為紅綠藍三種顏色的值)
第七個參數(shù):new Respone.ErrorListener<>并重寫Response.ErrorListener<>()方法,該方法為請求失敗的回調(diào)方法
⑵ImageLoader(步驟稍有不同)
1.創(chuàng)建請求隊列:
RequestQueue queue=Volley.RequestQueue(context);
2.new ImageLoader():
ImageLoader imageloader=new ImageLoader(queue,imageCache);
第一個參數(shù):請求隊列
第二個參數(shù):建立圖片緩存(重寫getBitmap(String)與putBitmap(String,Bitmap))
3.加載圖片:
ImageLoader.ImageListener imageListener=imageLoader.getImageListener(view,defaultImage,errorImage)
第一個參數(shù):顯示此圖片的控件id
第二個參數(shù):默認圖片
第三個參數(shù):加載錯誤時顯示的圖片
imageLoader.get(url,imageListener)
⑶NetworkImageView
1.顯示圖片的控件必須要用 <包名.NetworkImageView />格式創(chuàng)建
2.創(chuàng)建請求隊列
RequestQueue queue=Volley.RequestQueue(context);
3.創(chuàng)建ImageLoader
ImageLoader imageloader=new ImageLoader(queue,imageCache);
4.設(shè)置默認圖片和錯誤圖片
NetworkImageView_id.setDefaultImageResId(uri);
NetworkImageView_id.setErrorImageResId(uri);
5.加載圖片
NetworkImageView_id.setImageUrl(url,imageLoader);
注意:以上全部為GET請求,若為POST請求還需實現(xiàn)以下操作
若為POST請求還需要在創(chuàng)建請求(如StringRequest等)的方法塊中重寫
Map<String,String> getParams()
map中保存的為POST請求時需要上傳的數(shù)據(jù)
③將創(chuàng)建的請求添加到隊列中:
Context.queue.add(request);