Android就業面試技巧系列-技術篇(Volley框架)
Volley簡介:
比較流行的http開發庫。也封裝這個線程。
1.使用底層代碼來寫http請求 代碼量比較大。httpclient j2se httpURLConnection AscynHttpClient
2.高頻率http請求 優勢在處理數據量比較小的頻率高的請求。
3.劣勢 大文件的請求。大文件的上傳和下載。
1.使用底層代碼來寫http請求 代碼量比較大。httpclient j2se httpURLConnection AscynHttpClient
2.高頻率http請求 優勢在處理數據量比較小的頻率高的請求。
3.劣勢 大文件的請求。大文件的上傳和下載。
環境搭建:
1.創建一個工程
2.配置權限 Inernet
<!--聯網權限-->
<uses-permission android:name="android.permission.INTERNET" />
3.添加jar同時綁定源代碼
dependencies {
compile 'com.mcxiaoke.volley:library:1.0.19'
2.配置權限 Inernet
<!--聯網權限-->
<uses-permission android:name="android.permission.INTERNET" />
3.添加jar同時綁定源代碼
dependencies {
compile 'com.mcxiaoke.volley:library:1.0.19'
RequestQueue:
RequestQueue是一個請求隊列對象,它可以緩存所有的HTTP請求,然後按照一定的算法併發地發出這些請求。RequestQueue內部的設計就是非常合適高併發的,因此我們不必爲每一次HTTP請求都創建一個RequestQueue對象,這是非常浪費資源的,基本上在每一個需要和網絡交互的Activity中創建一個RequestQueue對象就足夠了。建議在Application子類中創建。
一.創建在Application http.itheima.com.http_volley.VolleyApplication
[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
public
class
VolleyApplication extends
Application{ //只要有一個組件啓動就可執行onCreate private
RequestQueue requestQueue; //提供get獲取方法 public
RequestQueue getInstance() { return
requestQueue; } @Override public
void
onCreate() { super .onCreate(); //創建一個核心類 requestQueue
= Volley.newRequestQueue( this ); //參1.Context上下文 } } |
二.在功能清單裏面配置 src/main/AndroidManifest.xml
<application
android:name="http.itheima.com.http_volley.VolleyApplication"
android:name="http.itheima.com.http_volley.VolleyApplication"
三 .提供get該法給其它的頁面使用。 http.itheima.com.http_volley.MainActivity
[Java] 純文本查看 複製代碼
1
2
3
4
5
|
//獲取應用上下文 VolleyApplication
application= (VolleyApplication) this .getApplication(); //返回請求隊表 RequestQueue
queue=application.getInstance(); Log.i( "MainActivity" ,queue+ "" ); |
案例1:
Request.Method volley的請求方式 常用的有get post
Response.Listener volley通過註冊監聽器獲取服務端返回的數據 (成功時候返回的數據)
Response.ErrorListener 網絡可能出現異常。volley也通過監聽器處理網絡異常(失敗時候返回的數據)
StringRequest volley提供的http請求對象的封裝。如果你要發送一個請求只需要創建Request的實現類的實例。
Response.Listener volley通過註冊監聽器獲取服務端返回的數據 (成功時候返回的數據)
Response.ErrorListener 網絡可能出現異常。volley也通過監聽器處理網絡異常(失敗時候返回的數據)
StringRequest volley提供的http請求對象的封裝。如果你要發送一個請求只需要創建Request的實現類的實例。
[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//創建一個請求 String
url= "http://192.168.1.114:8080/webapi//get?username=itheima" ; //根據返回結果創建請求類型 int
method= Request.Method.GET; //參1.請求方法
參數2.網絡連接的地址 參3.獲取數據成功的處理監聽器 Response.Listener
listener= new
Response.Listener<String>() { // @Override public
void
onResponse(String response) { //業務:展示給用戶 tvResponseString.setText(response); } }; //參4.網絡處理失敗監聽器 Response.ErrorListener
errorListener = new
Response.ErrorListener() { @Override public
void
onErrorResponse(VolleyError error) { //業務:展示組用戶網絡出錯的視圖 tvResponseString.setText( ">--<
網絡出錯了" +error.getMessage()); } }; //返回類型爲String請求 //Request:代表一個網絡請求 //
|--StringRequest 結果是一個String //
|--JsonReqeust 結果就是一個json 字符串 //
|--JsonArrayRequest 結果是一個json數據組對象 //
|--JSONObject 結果就是一個json對象 //
|--ImageRequest 結果是一張圖片 StringRequest
request= new
StringRequest(method,url,listener,errorListener); //只需添加請求隊列裏面由隊列自行處理 queue.add(request); |
案例二:
項目中一般使用它來完成登錄 。註冊等等。
volley封裝代碼非常簡潔。get post的底層差別是 對請求參數是否進行表單編碼。但是在代碼上的差異只有兩處:
1.參1使用 int method = Request.Method.POST;
2.重寫StringRequest的getParams返回表單數據 給底層。
3.不需要處理中文亂碼。
volley封裝代碼非常簡潔。get post的底層差別是 對請求參數是否進行表單編碼。但是在代碼上的差異只有兩處:
1.參1使用 int method = Request.Method.POST;
2.重寫StringRequest的getParams返回表單數據 給底層。
3.不需要處理中文亂碼。
[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
final
TextView tvResponseString = (TextView) findViewById(R.id.activity_main_tv_reponse_string); //獲取應用上下文 VolleyApplication
application = (VolleyApplication) this .getApplication(); //返回請求列表 RequestQueue
queue = application.getInstance(); Log.i( "MainActivity" ,
queue + "" ); String
url = "http://192.168.1.114:8080/webapi/post" ; //Volley把表單參數存在map final
HashMap<String, String> params = new
HashMap<>(); //
username=hh&password=123 params.put( "username" ,
"hh中國" ); params.put( "password" ,
"123" ); //創建String返回類型的請求 //參1. int
method = Request.Method.POST; //參2.地址 //參3. Response.Listener<String>
listener = new
Response.Listener<String>() { @Override public
void
onResponse(String response) { //業務
展示 //{"msg":"成功收到信息","data":"45cefcea-4090-4690-bea4-7351f72cd50d"} try
{ JSONObject
jsonObject = new
JSONObject(response); String
msg = jsonObject.getString( "msg" ); String
data = jsonObject.getString( "data" ); tvResponseString.setText(msg
+ "
"
+ data); }
catch
(JSONException e) { e.printStackTrace(); } } }; //參4 Response.ErrorListener
errorListener = new
Response.ErrorListener() { @Override public
void
onErrorResponse(VolleyError error) { tvResponseString.setText( "獲取數據失敗" ); } }; StringRequest
request = new
StringRequest(method, url,listener,errorListener){ @Override protected
Map<String, String> getParams() throws
AuthFailureError { //
重寫getParams() 方法 把參數放到請求裏面 return
params; //getParams發送請求時把數據取出放入請求裏 } }; //發送 queue.add(request); |
案例三.請求類型
volley爲了滿足項目中的常見數據返回類型提供了Request的幾個子類
//Request:代表一個網絡請求
// |--StringRequest 結果是一個String
// |--JsonReqeust 結果就是一個json 字符串
// |--JsonArrayRequest 結果是一個json數據組對象
// |--JSONObject 結果就是一個json對象
// |--ImageRequest 結果是一張圖片 圖片框架課
根據服務端返回的數據類型不同來創建上面的幾種請求方式 本例中以服務端返回的是String字符串,以創建的是StringRequest 請求
一般請求類型不同Response.Listener傳入參數也不同。
//Request:代表一個網絡請求
// |--StringRequest 結果是一個String
// |--JsonReqeust 結果就是一個json 字符串
// |--JsonArrayRequest 結果是一個json數據組對象
// |--JSONObject 結果就是一個json對象
// |--ImageRequest 結果是一張圖片 圖片框架課
根據服務端返回的數據類型不同來創建上面的幾種請求方式 本例中以服務端返回的是String字符串,以創建的是StringRequest 請求
一般請求類型不同Response.Listener傳入參數也不同。
實現代碼:
[Java] 純文本查看 複製代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
final
TextView tvResponseString= (TextView) findViewById(R.id.activity_main_tv_reponse_string); //獲取應用上下文 VolleyApplication
application= (VolleyApplication) this .getApplication(); //返回請求列表 RequestQueue
queue=application.getInstance(); Log.i( "MainActivity" ,queue+ "" ); //創建一個請求 String
url= "http://192.168.1.114:8080/webapi//get?username=itheima" ; //根據返回結果創建請求類型 int
method= Request.Method.GET; //參1.請求方法
參數2.網絡連接的地址 參3.獲取數據成功的處理監聽器 Response.Listener<JSONObject>
listener= new
Response.Listener<JSONObject>() { // @Override public
void
onResponse(JSONObject response) { //服務端返回的數據 //{"msg":"成功收到信息","data":{"username":"\"黑馬\""}} try
{ String
msg= response.getString( "msg" ); String
username= response.getJSONObject( "data" ).getString( "username" ); //業務:展示給用戶 tvResponseString.setText(msg+ "
" +username); }
catch
(JSONException e) { e.printStackTrace(); |