在学习郭霖前辈的博客(http://blog.csdn.net/guolin_blog/article/details/17482095)后自己做的简要学习笔记,为的是提炼下思路,在心里能有个整体的框架。
由浅入深:
一、StringRequest使用步骤:
1、定义消息队列:RequestQueue;
2、定义StringRequest,传入网址,监听方法等;
3、用队列加载请求(add方法)
二、ImageRequest使用步骤:
1、定义RequestQueue
2、定义ImageRequest,传入url,Listener,长,宽,颜色属性,onErrorResponse
3、用队列Queue加载ImageRequest(add方法)
public RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
Log.v("TAG","onResponse");
mImageView.setImageBitmap(bitmap);
Toast.makeText(ImageVolley.this, "获取成功!", Toast.LENGTH_LONG).show();
}
}, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.v("TAG","onErrorResponse");
Toast.makeText(ImageVolley.this, "获取图片失败,请重新加载!", Toast.LENGTH_LONG).show();
}
});
mQueue.add(imageRequest);
三、ImageLoader:
1、定义RequestQueue;
2、定义ImageLoader;
3、定义一个ImageListener(既可以自己写新的ImageListener,也可以用ImageLoader.getImageListener方法,后者肯定更简单一点,一句话就行)
4、用ImageLoader的get方法加载网址和ImageListener
代码如下:
public RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
ImageLoader imageLoader = new ImageLoader(mQueue,new BitmapCache());
//自己写的ImageListener
/* ImageLoader.ImageListener listener = new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
Log.v("TAG","onResponse");
mImageView.setImageBitmap(imageContainer.getBitmap());
Toast.makeText(ImageVolley.this,"获取成功!",Toast.LENGTH_LONG).show();
}
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.v("TAG","onErrorResponse");
Toast.makeText(ImageVolley.this,"获取失败,请重新加载!",Toast.LENGTH_LONG).show();
}
};*/
//用的ImageLoader的getImageListener,更方便一点,不用自己写;
ImageLoader.ImageListener listener = ImageLoader.getImageListener(mImageView,R.drawable.illustrator,R.drawable.jelly01);
imageLoader.get(url,listener);
四、自定义XmlRequest
参考了StringRequest的定义,来写一个继承自Request的XmlRequest类,照StringRequest葫芦画瓢写构造方法,以及实现parseNetworkResponse函数,因为用的是pull方法解析xml文件,所以参数类型是XmlPullParser类型,主要代码如下:
@Override
protected Response<XmlPullParser> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String xmlString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlString));
return Response.success(xmlPullParser,HttpHeaderParser.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
} catch (XmlPullParserException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
五、自定义GsonRequest
下载一个Gson的jar包,放在libs下,然后在AS的Project目录里右击它,Add to Library,就可以用Gson了,因为有Google的Gson,所以很方便写自定义的GsonRequest:
@Override
protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
return Response.success(mGson.fromJson(jsonString,mClass),HttpHeaderParser.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}