这里,我就上传源码,然后一一讲解每个方法的使用;
因为基本可能每个Activity或者Fragment都可能需要访问网络,不可能在每个活动界面都重新写一遍访问的代码,这样会后期的维护,重构都不是最好的选择。所以,能封装的就基本都进行封装。以后更改,更新都是非常不错的选择!
import java.util.Map;
import org.json.JSONObject;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.LruCache;
import android.widget.ImageView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
/**
*
* @author hzx
* @category volley网络请求工具
*
*/
public class VolleyHttpUtils {
private static VolleyHttpUtils mintance = null;
private Context mcontext;
private static RequestQueue mqueue;
//private ImageLoader imageLoader;
private static Map<String, String> map;
private static final int maxSize = 10 * 1024 *1024;
private VolleyHttpUtils(Context mcontext){
this.setMcontext(mcontext);
mqueue = Volley.newRequestQueue(mcontext.getApplicationContext());
}
public static VolleyHttpUtils getIntance(Context context){
if(mintance == null){
synchronized (VolleyHttpUtils.class) {
if(mintance == null){
mintance = new VolleyHttpUtils(context);
}
}
}
return mintance;
}
/**
* post请求
* @param url
* @param mListenter
* @param error
*/
public void getPost(String url,Listener<String> mListenter,ErrorListener error){
StringRequest request = new StringRequest(Method.POST, url, mListenter, error){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return getMap();
}
};
mqueue.add(request);
}
/**
* JSONObjectRequest
* @param url
* @param jos
* @param mListener
* @param merror
*/
public static void getJsonObjectRequest(String url,JSONObject jos,Listener<JSONObject> mListener,ErrorListener merror){
JsonObjectRequest jor = new JsonObjectRequest(Method.POST, url,jos, mListener, merror);
mqueue.add(jor);
}
/**
* JSONObjectRequest
* @param url
* @param mListener
* @param merror
*/
public static void getJsonObjectRequest(String url,Listener<JSONObject> mListener,ErrorListener merror){
JsonObjectRequest jor = new JsonObjectRequest(Method.POST, url, null, mListener, merror);
mqueue.add(jor);
}
/**
* Request image load,don't neet to cache,
* @param url
* @param mListener
* @param merror
*/
public static void getImageBitmapNoCache(String url,Listener<Bitmap> mListener,ErrorListener merror){
ImageRequest imageRequest = new ImageRequest(url, mListener, 0, 0, Config.RGB_565, merror);
mqueue.add(imageRequest);
}
/**
* download image,neet to cache;
* @param imageview
* @param url
* @param mListener
* @param errorImageId
* @param defaultImageId
* @param mError
*/
public static void getImageBimapCache(ImageView imageview,String url,Listener<Bitmap> mListener,int errorImageId,int defaultImageId,ErrorListener mError){
ImageLoader imageLoader = new ImageLoader(mqueue,new ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(maxSize);
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
});
ImageListener imageListener = ImageLoader.getImageListener(imageview, errorImageId, defaultImageId);
imageLoader.get(url, imageListener, 200, 200);
}
/**
* net load image,neet to cache;
* @param networkImage
* @param url
* @param mListener
* @param errorImageId
* @param defaultImageId
* @param mError
*/
public static void getNetworkImageBitmapCacher(NetworkImageView networkImage,String url,Listener<Bitmap> mListener,int errorImageId,int defaultImageId,ErrorListener mError){
ImageLoader imagerloader = new ImageLoader(mqueue,new ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(maxSize);
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
});
networkImage.setDefaultImageResId(defaultImageId);
networkImage.setErrorImageResId(errorImageId);
networkImage.setImageUrl(url, imagerloader);
}
public static void stopLoadNetRequest(){
mqueue.stop();
}
public void setMap(Map<String, String> map){
VolleyHttpUtils.map = map;
}
private static Map<String, String> getMap(){
return map;
}
public Context getMcontext() {
return mcontext;
}
public void setMcontext(Context mcontext) {
this.mcontext = mcontext;
}
}
整个封装的过程,我是使用了,单例设计模式,但这种设计模式,也不是尽善完美的。多多少少还是有点缺陷;不管怎么样,使用单例设计模式不用每个使用的地方都重新去new一遍,然后,产生多个实例,对应用程序的性能不是特别的好;
每个方法都是需要传入不同的参数的,都有一定的说明。
使用post请求的时候,需要首先传入map参数,不然map参数为空,访问将会出错。
public void getjsonString(String url){
Map<String, String> map = new HashMap<String, String>();
VolleyHttpUtils.getIntance(getApplicationContext()).setMap(map);
VolleyHttpUtils.getIntance(getApplicationContext()).getPost(url, new Listener<String>() {
@Override
public void onResponse(String response) {
//UI线程
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//产生错误信息
}
});
}