public class MainActivity extends Activity { RequestQueue mQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQueue = Volley.newRequestQueue(this); getVolley(); postVolley(); } //get方法 private void getVolley() { StringRequest stringRequest = new StringRequest("http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String arg0) { System.out.println("########## " + arg0); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println("$$$$$$$$$$ " + arg0); } }); mQueue.add(stringRequest); }
//post方法 private void postVolley() { StringRequest request = new StringRequest(Method.POST, "http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String arg0) { System.out.println("@@@@@@@@@@@@ " + arg0); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println("%%%%%%%%%%% " + arg0); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("Key1", "value1"); map.put("Key2", "value2"); return map; } }; mQueue.add(request); } }
post方法,有的同窗複寫不了getParams()方法,是由於須要實現一個匿名內部類。json
寫法:api
//post方法
private void postVolley() {
StringRequest request = new StringRequest(method, url, listener, errorListener){
此時,在此處@Override就能夠看到Request<String>了,而後點開選擇getParams()方法就能夠了。緩存
};
}網絡
public class MainActivity extends Activity { RequestQueue mQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQueue = Volley.newRequestQueue(this); jsonVolley(); } private void jsonVolley() { JsonObjectRequest jb = new JsonObjectRequest("http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject arg0) { System.out.println("########## " + arg0); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println("$$$$$$$$$$ " + arg0); } }); mQueue.add(jb); } }
從獲取圖片開始,咱們RequestQueue放入Application,由於一個程序中一般只須要一個RequestQueue實例。app
public class ApplicationTest extends Application { private static ApplicationTest instance; private RequestQueue mQueue; public static ApplicationTest getInstance() { if (instance == null) { instance = new ApplicationTest(); } return instance; } @Override public void onCreate() { super.onCreate(); instance = this; setmQueue(Volley.newRequestQueue(this)); } public RequestQueue getmQueue() { return mQueue; } public void setmQueue(RequestQueue mQueue) { this.mQueue = mQueue; } }
public class ImgActivity extends Activity { ApplicationTest app; RequestQueue mQ; ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQ = app.getInstance().getmQueue(); iv = (ImageView) findViewById(R.id.img); imgVolley(); } // ImageRequest的構造函數接收六個參數,第一個參數就是圖片的URL地址,這個沒什麼須要解釋的。第二個參數是圖片請求成功的回調,這裏咱們把返回的Bitmap參數設置到ImageView中。第三第四個參數分別用於指定容許圖片最大的寬度和高度,若是指定的網絡圖片的寬度或高度大於這裏的最大值,則會對圖片進行壓縮,指定成0的話就表示無論圖片有多大,都不會進行壓縮。第五個參數用於指定圖片的顏色屬性,Bitmap.Config下的幾個常量均可以在這裏使用,其中ARGB_8888能夠展現最好的顏色屬性,每一個圖片像素佔據4個字節的大小,而RGB_565則表示每一個圖片像素佔據2個字節大小。第六個參數是圖片請求失敗的回調,這裏咱們當請求失敗時在ImageView中顯示一張默認圖片。 @SuppressWarnings("deprecation") private void imgVolley() { ImageRequest img = new ImageRequest( "http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg", new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap arg0) { iv.setImageBitmap(arg0); } }, 0, 0, Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { iv.setImageResource(R.drawable.ic_launcher); } }); mQ.add(img); } }
若是你以爲ImageRequest已經很是好用了,那我只能說你太容易知足了 ^_^。實際上,Volley在請求網絡圖片方面能夠作到的還遠遠不止這些,而ImageLoader就是一個很好的例子。ImageLoader也能夠用於加載網絡上的圖片,而且它的內部也是使用ImageRequest來實現的,不過ImageLoader明顯要比ImageRequest更加高效,由於它不只能夠幫咱們對圖片進行緩存,還能夠過濾掉重複的連接,避免重複發送請求。ide
public class ImgActivity extends Activity { ApplicationTest app; RequestQueue mQ; ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQ = app.getInstance().getmQueue(); iv = (ImageView) findViewById(R.id.img); loaderImgVolley(); } private void loaderImgVolley() { ImageLoader imgLoader = new ImageLoader(mQ, new ImageCache() { @Override public void putBitmap(String arg0, Bitmap arg1) { // 若是不緩存,能夠空着 } @Override public Bitmap getBitmap(String arg0) { // TODO Auto-generated method stub return null; } }); ImageListener listener = ImageLoader.getImageListener(iv, R.drawable.app_icon, R.drawable.ic_launcher); imgLoader.get("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", listener); } }
雖然如今咱們已經掌握了ImageLoader的用法,可是剛纔介紹的ImageLoader的優勢卻尚未使用到。爲何呢?由於這裏建立的ImageCache對象是一個空的實現,徹底沒能起到圖片緩存的做用。其實寫一個ImageCache也很是簡單,可是若是想要寫一個性能很是好的ImageCache,最好就要藉助Android提供的LruCache功能.函數
public class ImgActivity extends Activity { ApplicationTest app; RequestQueue mQ; ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQ = app.getInstance().getmQueue(); iv = (ImageView) findViewById(R.id.img); loaderImgVolley(); } /** *圖片緩存須要作成單例,全局共享 */ private static class LruImageCache implements ImageCache { private LruImageCache(){} private static LruImageCache instance = new LruImageCache(); public static final LruImageCache getInstance() { return instance; } private static final String TAG = "LruImageCache"; private final int maxSize = (int) (Runtime.getRuntime().maxMemory()/8); private LruCache<String,Bitmap> mCacheMap = new LruCache<String,Bitmap>(maxSize) { protected int sizeOf(String key, Bitmap value) { return value.getRowBytes()*value.getHeight(); } }; @Override public Bitmap getBitmap(String url) { Bitmap bitmap = mCacheMap.get(url); Log.i(TAG, "url = "+url); return bitmap; } @Override public void putBitmap(String url, Bitmap bitmap) { Log.i(TAG, "put url = "+url+",cache:"+bitmap); mCacheMap.put(url, bitmap); } } private void loaderImgVolley() { ImageLoader imgLoader = new ImageLoader(mQ, new LruImageCache()); ImageListener listener = ImageLoader.getImageListener(iv, R.drawable.app_icon, R.drawable.ic_launcher); imgLoader.get("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", listener); } }