天天看點

Volley網絡連接配接

一、Volley

a burst or emission of many things or a large amount at once

Volley是Android平台上的網絡通信庫,能使網絡通信更快,更簡單,更健壯。

二、特點

異步任務下載下傳圖檔的操作存在幾個問題

1、  代碼量大且繁瑣

2、  ListView滾動太快,可能導緻下載下傳的圖檔無法正常顯示

3、  可能浪費系統資源

4、  旋轉螢幕可能導緻再次下載下傳

由此提出使用Volley替代 網絡操作

但是隻适合簡單的網絡操作:

1、  json/xml文本資料

2、  圖檔加載

不能用于大資料的下載下傳 和 檔案的上傳

三、使用前準備

找到volley檔案 (sdk版本檔案下com/android/volley)

将volley檔案内的内容(所有檔案)複制到項目com.android.volley包下

       删除類名帶有Text 的測試java檔案

四、下載下傳文本資料的方法

1、StringRequest

Volley網絡連接配接
Volley網絡連接配接

1 package com.xqx.volleydemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 
 7 import com.android.volley.RequestQueue;
 8 import com.android.volley.Response;
 9 import com.android.volley.toolbox.JsonArrayRequest;
10 import com.android.volley.toolbox.StringRequest;
11 import com.android.volley.toolbox.Volley;
12 
13 public class MainActivity extends Activity {
14     
15     //1、聲明RequestQueue
16     private RequestQueue requestQueue;
17     private TextView tv_show;
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         String url = "http://ikft.house.qq.com/index.php?guid=866500021200250&devua=appkft_1080_1920_XiaomiMI4LTE_1.8.3_Android19&order=0&searchtype=normal&devid=866500021200250&appname=QQHouse&mod=appkft&act=searchhouse&channel=71&page=1&rn=20&cityid=1";
23         tv_show = (TextView) findViewById(R.id.tv_show);
24         //2、執行個體化RequestQueue對象
25         requestQueue = Volley.newRequestQueue(this);
26         //下載下傳資料,傳回字元串格式的資料
27         StringRequest request = new StringRequest(url, new Response.Listener<String>() {
28             @Override
29             public void onResponse(String response) {
30                 //得到字元串資料response
31                 tv_show.setText(response);
32             }
33         }, null);
34         //3、将請求添加到隊列中
35         requestQueue.add(request);
36     }
37 
38 }      

MainActivity.java

Volley網絡連接配接
Volley網絡連接配接
1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3               android:orientation="vertical"
 4               android:layout_width="fill_parent"
 5               android:layout_height="fill_parent"
 6         >
 7 
 8     <TextView
 9             android:layout_gravity="center"
10             android:gravity="center"
11             android:layout_width="fill_parent"
12             android:layout_height="wrap_content"
13             android:text="下載下傳的内容"
14             android:id="@+id/tv_show"
15             />
16     
17     
18 </FrameLayout>      

activity_main

 2、JsonObjectRequest

Volley網絡連接配接
Volley網絡連接配接
1 JsonObjectRequest request=new JsonObjectRequest(Method.GET, url, null,
 2                 new Response.Listener<JSONObject>() {
 3                     @Override
 4                     public void onResponse(JSONObject response) {
 5                         // TODO 請求成功
 6                         try {
 7                             JSONArray array=response.getJSONArray("data");
 8                             parseJson(array);
 9                         } catch (JSONException e) {
10                             e.printStackTrace();
11                         }
12                         
13                     }
14                 }, new Response.ErrorListener() {
15                     @Override
16                     public void onErrorResponse(VolleyError error) {
17                         // TODO Auto-generated method stub
18                         Toast.makeText(getApplicationContext(), "請求出錯", 0).show();
19                     }
20                 });      

method

五、加載圖檔的方法

1、ImageRequest 

Volley網絡連接配接
Volley網絡連接配接
1 package com.xqx.volleydemo;
 2 
 3 import android.app.Activity;
 4 import android.graphics.Bitmap;
 5 import android.os.Bundle;
 6 import android.widget.ImageView;
 7 
 8 import com.android.volley.RequestQueue;
 9 import com.android.volley.Response;
10 import com.android.volley.VolleyError;
11 import com.android.volley.toolbox.ImageRequest;
12 import com.android.volley.toolbox.Volley;
13 
14 public class MainActivity extends Activity {
15     //1、聲明RequestQueue
16     private RequestQueue requestQueue;
17     private ImageView img_show;
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         img_show = (ImageView) findViewById(R.id.img_show);
23         //2、執行個體化RequestQueue對象
24         requestQueue = Volley.newRequestQueue(this);
25         //加載圖檔
26         ImageRequest request = new ImageRequest("http://www.baidu.com/img/bd_logo.png",
27                 new Response.Listener<Bitmap>() {
28                     @Override
29                     public void onResponse(Bitmap response) {
30                     //圖檔下載下傳成功後回調此方法
31                         //TODO 設定ImageView
32                         img_show.setImageBitmap(response);
33                     }
34                 },
35                 //記憶體中Bitmap最大的寬度,高度限制,用于降低記憶體的消耗
36                 128, 64,
37                 //告訴BitmapFactory 在生産Bitmap的時候一個像素包含的資訊
38                 Bitmap.Config.ARGB_8888,
39                 //圖檔加載失敗的時候回調
40                 new Response.ErrorListener() {
41                     @Override
42                     public void onErrorResponse(VolleyError error) {
43                         //TODO 顯示加載失敗的圖檔
44                         img_show.setImageResource(R.drawable.ic_launcher);
45                     }
46                 }
47         );
48         //3、将請求添加到隊列中
49         requestQueue.add(request);
50     }
51 }      
Volley網絡連接配接
Volley網絡連接配接
1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3               android:orientation="vertical"
 4               android:layout_width="fill_parent"
 5               android:layout_height="fill_parent"
 6         >
 7 
 8     
 9     <ImageView 
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:id="@+id/img_show"
13         />
14 </FrameLayout>      

activity_main.xml

2、ImageLoader

Volley網絡連接配接
Volley網絡連接配接
1 package com.xqx.volleydemo;
 2 
 3 import android.app.Activity;
 4 import android.graphics.Bitmap;
 5 import android.os.Bundle;
 6 import android.util.LruCache;
 7 import android.widget.ImageView;
 8 
 9 import com.android.volley.RequestQueue;
10 import com.android.volley.toolbox.ImageLoader;
11 import com.android.volley.toolbox.Volley;
12 
13 public class MainActivity extends Activity {
14     
15     //1、聲明RequestQueue
16     private RequestQueue requestQueue;
17     private ImageLoader imageloder;
18     private ImageView imgView;
19     @Override
20     public void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         imgView = (ImageView) findViewById(R.id.img_show);
24         
25         
26         imageloder = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
27 
28             private LruCache<String,Bitmap> cache = new LruCache<>(10);
29 
30             @Override
31             public Bitmap getBitmap(String url) {
32 
33                 return cache.get(url);
34             }
35 
36             @Override
37             public void putBitmap(String url, Bitmap bitmap) {
38                 cache.put(url,bitmap);
39             }
40         });
41         imageloder.get("http://www.baidu.com/img/bd_logo.png"
42                 , ImageLoader.getImageListener(imgView,R.drawable.ic_launcher,
43                 android.R.drawable.ic_media_pause));
44     }
45 
46 }      
Volley網絡連接配接
Volley網絡連接配接
1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3               android:orientation="vertical"
 4               android:layout_width="fill_parent"
 5               android:layout_height="fill_parent"
 6         >
 7 
 8 
 9     <ImageView 
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:id="@+id/img_show"
13         />
14 </FrameLayout>