标签:
<uses-permissionandroid:name="android.permission.INTERNET"/>
private RequestQueue mRequestQueue; //创建一个RequestQueue mRequestQueue = Volley.newRequestQueue(this); //创建一个StringRequest---GET请求 String url = "http://www.baidu.com"; StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG, response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, error.getMessage()); } }); //将StringRequest对象添加到RequestQueue mRequestQueue.add(stringRequest);3、结果将会得到一串Html源码字符串
String jsonurl = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json"; Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i(TAG, response.toString()); } }; Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG, error.getMessage()); } }; JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, jsonurl, null, listener, errorListener); mRequestQueue.add(jr);(三)简单的POST请求
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, jsonurl, null, listener, errorListener) { //将Post的参数写入到map中 @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("params1", "value1"); map.put("params2", "value2"); return map; } }; mRequestQueue.add(postRequest);
String imageUrl = "http://a.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec5cfb43e02d2eb9389b506b89.jpg"; int max_width = 300; int max_height = 300; Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { myImageView.setImageBitmap(response); } }; Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { myImageView.setImageResource(R.drawable.default_pg); } }; ImageRequest imageRequest = new ImageRequest(imageUrl, listener, max_width, max_height, Config.RGB_565, errorListener); mRequestQueue.add(imageRequest);其中,ImageRequest中的参数意义分别为:
* @param url URL of the image * @param listener Listener to receive the decoded bitmap * @param maxWidth Maximum width to decode this bitmap to, or zero for none * @param maxHeight Maximum height to decode this bitmap to, or zero for * none * @param decodeConfig Format to decode the bitmap to * @param errorListener Error listener, or null to ignore errors
//第一步创建RequestQueue,前面已经创建,这里不再重复创建 String imageUrl = "http://a.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec5cfb43e02d2eb9389b506b89.jpg"; //创建LruCache保存下载过的图片 final LruCache<String, Bitmap> mImageLruCache = new LruCache<String, Bitmap>(20); //创建ImageCache,供后面使用 ImageCache imageCache = new ImageCache() { @Override public void putBitmap(String url, Bitmap bitmap) { mImageLruCache.put(url, bitmap); } @Override public Bitmap getBitmap(String url) { return mImageLruCache.get(url); } }; //创建ImageLoader ImageLoader imageLoader = new ImageLoader(mRequestQueue, imageCache); /** * @param imageView The imageView that the listener is associated with. * @param defaultImageResId Default image resource ID to use, or 0 if it doesn't exist. * @param errorImageResId Error image resource ID to use, or 0 if it doesn't exist. */ //获取一个ImageListener对象 ImageListener imageListener = ImageLoader.getImageListener(myImageView, R.drawable.default_pg, R.drawable.failed_pg); //调用ImageLoader的get()方法加载网络上的图片 imageLoader.get(imageUrl, imageListener); //如果想对加载图片大小进行限定,使用get()的另一个重载方式 imageLoader.get(imageUrl, imageListener, 300, 300);ImageLoader能够提供的一个比较好的实践就是能够提供图片缓存,这里通过LruCache来实现,最大缓存的图片数目为20张;我们也可以自定义最大缓存图片的内存大小,如下所示:
final LruCache<String, Bitmap> mImageLruCache = new LruCache<String, Bitmap>(maxSize){ @Override protected int sizeOf(String url, Bitmap bitmap){ return bitmap.getRowBytes() * bitmap.getHeight(); } }; ImageCache imageCache = new ImageCache() { @Override public void putBitmap(String url, Bitmap bitmap) { mImageLruCache.put(url, bitmap); } @Override public Bitmap getBitmap(String url) { return mImageLruCache.get(url); } };
<com.android.volley.toolbox.NetworkImageView android:id="@+id/myNetworkImageView" android:layout_width="300dp" android:layout_height="300dp" android:contentDescription="@string/hello_world" />
//前面创建ImageLoader与前面相同,不再重复创建 //使用NetworkImageView NetworkImageView imageView = (NetworkImageView) findViewById(R.id.myNetworkImageView); imageView.setDefaultImageResId(R.drawable.default_pg); imageView.setErrorImageResId(R.drawable.failed_pg); imageView.setImageUrl(imageUrl, imageLoader);
标签:
原文地址:http://blog.csdn.net/woliuyunyicai/article/details/46529819