码迷,mamicode.com
首页 > 其他好文 > 详细

Volley 应用

时间:2017-06-10 16:51:49      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:view   适合   优先级   处理   sys   protect   cti   mooc   date   

* Volley是Android平台的网络通信库:更快,更简单,更健壮
* Volley提供的功能:
* 1:JSON,图片(异步)
* 2.网络请求排序
* 3.网络请求的优先级处理
* 4.缓存
* 5.多级别的取消请求
* 6.与Activity生命周期联动

 适合:频繁的,数据量比较小的请求

//Volley实现JSON字符串请求

 1 //Volley实现JSON字符串请求
 2 //最小sdk8.0以上
 3 public class MainActivity extends Activity {
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.fragment_main);
 9         getJSONVolley();
10 
11     }
12 
13     // 获取json字符串
14     public void getJSONVolley() {
15 
16         RequestQueue requestQueue = Volley.newRequestQueue(this);
17 
18         String JSONDateUrl = "http://www.imooc.com/api/teacher?type=4&num=30";
19         JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
20                 Request.Method.GET, JSONDateUrl, null,
21                 new Response.Listener<JSONObject>() {
22                     public void onResponse(JSONObject response) {
23 
24                         System.out.println("--->>response:" + response);
25                     }
26                 }, new Response.ErrorListener() {
27                     public void onErrorResponse(
28                             com.android.volley.VolleyError error) {
29 
30                         System.out.println("--->>error");
31                     }
32                 });
33  
34         requestQueue.add(jsonObjectRequest);
35     }
36 }

 

 

 

 

 1 ////使用Volley从服务器下载图片
 2 //最小sdk8.0以上
 3 public class MainActivity extends Activity {
 4     ImageView imageView;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.fragment_main);
10         imageView = (ImageView) findViewById(R.id.imageView1);
11         loadImageVolley();
12 
13     }
14 
15     // 使用Volley从服务器下载图片
16     // http://localhost:8080/web/Sky.jpg
17     public void loadImageVolley() {
18         String imageurl = "http://172.16.166.195:8080/web/Sky.jpg";
19         RequestQueue requestQueue = Volley.newRequestQueue(this);
20         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
21                 20);
22         ImageCache imageCache = new ImageCache() {
23 
24             @Override
25             public void putBitmap(String url, Bitmap bitmap) {
26                 // TODO Auto-generated method stub
27                 lruCache.put(url, bitmap);
28             }
29 
30             @Override
31             public Bitmap getBitmap(String url) {
32                 // TODO Auto-generated method stub
33                 return lruCache.get(url);
34             }
35         };
36         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
37         ImageListener listener = imageLoader.getImageListener(imageView,
38                 R.drawable.ic_launcher, R.drawable.ic_launcher);
39         imageLoader.get(imageurl, listener);
40 
41     }
42 }

技术分享

 1 //使用 NetWorkImageView 完成图片加载
 2 //最小sdk8.0以上
 3 public class MainActivity extends Activity {
 4     NetworkImageView imageView;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.fragment_main);
10         imageView = (NetworkImageView) findViewById(R.id.imageView1);
11         NetWorkImageViewVolley();
12 
13     }
14 
15     public void NetWorkImageViewVolley() {
16         String imageurl = "http://172.16.166.195:8080/web/Sky.jpg";
17         RequestQueue requestQueue = Volley.newRequestQueue(this);
18         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
19                 20);
20         ImageCache imageCache = new ImageCache() {
21 
22             @Override
23             public void putBitmap(String url, Bitmap bitmap) {
24                 // TODO Auto-generated method stub
25                 lruCache.put(url, bitmap);
26             }
27 
28             @Override
29             public Bitmap getBitmap(String url) {
30                 // TODO Auto-generated method stub
31                 return lruCache.get(url);
32             }
33 
34         };
35         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
36 
37         imageView.setTag("url");
38         imageView.setImageUrl(imageurl, imageLoader);
39 
40     }
41 }

技术分享

 

Volley 应用

标签:view   适合   优先级   处理   sys   protect   cti   mooc   date   

原文地址:http://www.cnblogs.com/my334420/p/6978837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!