關(guān)鍵字: Volley
JsonObject
JsonArrayRequest
StringRequest
Google官方Volley介紹圖片
前言:這篇文章也是記錄我在慕課網(wǎng)跟隨
JVR
老師學(xué)習(xí)到有關(guān)Volley的知識(shí)。關(guān)于Volley的記錄將分為兩部分,分別記錄“Volley網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求的使用”和“使用Volley加載础芍、緩存圖片”。這篇文章是第一部分寒瓦。
這里給出有關(guān)鏈接于颖,方便大家訪問迟几。
慕課網(wǎng):http://www.imooc.com/
JVR老師慕課網(wǎng)個(gè)人主頁(yè):http://www.imooc.com/space/teacher/id/2225574
正文:
Volley
的中文翻譯為齊射消请、并發(fā),是Google2013年io大會(huì)上發(fā)布的網(wǎng)絡(luò)通訊庫(kù)类腮。
- 通信更快臊泰、更簡(jiǎn)單
- GET、POST網(wǎng)絡(luò)請(qǐng)求及網(wǎng)絡(luò)圖像的高效率異步處理請(qǐng)求
- 對(duì)請(qǐng)求進(jìn)行優(yōu)先級(jí)的排序
- 網(wǎng)絡(luò)請(qǐng)求的緩存
- 多級(jí)別取消請(qǐng)求
- 和Activity生命周期的聯(lián)動(dòng)
- 不太適合進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的上傳和下載
- Volley提供了這么幾個(gè)請(qǐng)求對(duì)象:
StringRequest
:返回的結(jié)果不確定時(shí)使用
JsonObjectRequest
:返回結(jié)果為JsonObject蚜枢,直接使用該對(duì)象因宇,方便封裝數(shù)據(jù)
JsonArrayRequest
:返回結(jié)果為JsonArray,直接使用該對(duì)象祟偷,方便封裝數(shù)據(jù) - Volley提供了一些使用方便的回調(diào)方法:
onResponse
:在請(qǐng)求成功時(shí)調(diào)用
onErrorResponse
:在請(qǐng)求失敗時(shí)調(diào)用 - 隊(duì)列建立和取消隊(duì)列請(qǐng)求
- 和Activity生命周期的聯(lián)動(dòng)
可以在Activity銷毀的時(shí)候,關(guān)閉請(qǐng)求打厘,一般是使用Tag
標(biāo)簽修肠,在onStop()
里取消請(qǐng)求
Volley網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求的使用
要使用 Volley,先要在項(xiàng)目中引入Volley庫(kù)
在build.gradle
文件中添加Volley庫(kù)户盯,Android studio會(huì)自動(dòng)下載嵌施。
dependencies {
compile 'com.mcxiaoke.volley:library:1.0.17'
}
1. 使用GET方式請(qǐng)求
//按鈕的點(diǎn)擊事件
public void click(View view) {
// 要訪問的URL
String url="http://m.weather.com.cn/atad/101010100.html";
//Volley的請(qǐng)求隊(duì)列
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
//StringRequest實(shí)例
//第一個(gè)參數(shù):請(qǐng)求方式GET、POST
//第二個(gè)參數(shù):請(qǐng)求的URL
//第三個(gè)參數(shù):請(qǐng)求成功的監(jiān)聽器莽鸭,傳遞了請(qǐng)求結(jié)果
//第四個(gè)參數(shù):請(qǐng)求失敗的監(jiān)聽器吗伤,傳遞了失敗的原因
StringRequest stringRequest = new StringRequest(Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//使用獲取的數(shù)據(jù)更新UI
button.setText(response); }
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
button.setText(error.toString());
}
}) };
//設(shè)置該請(qǐng)求的Tag
stringRequest.setTag("StringGET");
//把該請(qǐng)求添加到requestQueue中
requestQueue.add(stringRequest);
//requestQueue.start();
}
如果使用JsonObjectRequest
請(qǐng)求對(duì)象,返回的response是JSONObject
對(duì)象硫眨,直接可以解析:
button.setText(response.getJSONObject("weatherinfo").get("city").toString());
JsonArrayRequest
請(qǐng)求對(duì)象類似足淆。
2. 使用POST方式請(qǐng)求
有兩種方法可以post參數(shù)。
- 通過重寫請(qǐng)求對(duì)象的
getParams()
方法礁阁,該方法在post請(qǐng)求時(shí)巧号,自動(dòng)調(diào)用,獲取請(qǐng)求參數(shù)姥闭。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST,
url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<>();
map.put("username","liming");
map.put("password", "123456");
return map;
}
};
- 如果是
JsonObjectRequest
還可以使用JsonObjectRequest
另一個(gè)構(gòu)造方法實(shí)現(xiàn)參數(shù)的傳遞:
public void click(View view) {
String url = "http://m.weather.com.cn/atad/101010100.html";
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
//封裝請(qǐng)求參數(shù)
Map<String, String>map = new HashMap<>();
map.put("username","liming");
map.put("password","123456");
JSONObject jsonObject = new JSONObject(map);
//參數(shù)jsonObject封裝了請(qǐng)求參數(shù)丹鸿,Volley會(huì)自動(dòng)提取里面的值,作為post請(qǐng)求參數(shù)
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST,
url,jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//執(zhí)行請(qǐng)求成功的邏輯
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//執(zhí)行請(qǐng)求失敗的邏輯
}
});
jsonObjectRequest.setTag("jsonObjectGET");
requestQueue.add(jsonObjectRequest);
//requestQueue.start();}