Volley网络连接

简介: 一、Volley a burst or emission of many things or a large amount at once Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。

一、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

 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
 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

 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 

 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 }
MainActivity.java
 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

 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 }
MainActivity.java
 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

 

相关文章
|
缓存 Java
浅谈Volley请求
浅谈Volley请求
119 0
浅谈Volley请求
|
数据格式 JSON
OkHttp网络连接封装工具类
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/50614615     最近,临近春节放假,把自己最近项目中使用 的okhttp使用做个总结,将自己的工具类贡献出来。
2072 0