Volley 應用

* Volley是Android平臺的網絡通訊庫:更快,更簡單,更健壯
* Volley提供的功能:
* 1:JSON,圖片(異步)
* 2.網絡請求排序
* 3.網絡請求的優先級處理
* 4.緩存
* 5.多級別的取消請求
* 6.與Activity生命週期聯動android

 適合:頻繁的,數據量比較小的請求web

//Volley實現JSON字符串請求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 }

相關文章
相關標籤/搜索