公共代码参考(Volley)

Volley 是google提供的一个网络库,相对于自己写httpclient确实方便很多,本文参考部分网上例子整理如下,以作备忘:

  • 定义一个缓存类:
public class BitmapCache implements ImageCache {

    private LruCache<String, Bitmap> cache;  
    
    public BitmapCache() {
        /*LruCache使用一个LinkedHashMap简单的实现内存的缓存,
        没有软引用,都是强引用。如果添加的数据大于设置的最大值,就删除最先缓存的数据来调整内存。*/
        cache = new LruCache<String, Bitmap>(8 * 1024 * 1024) {  
            @Override  
            protected int sizeOf(String key, Bitmap bitmap) {  
                return bitmap.getRowBytes() * bitmap.getHeight();  
            }  
        };  
    }  
  
    @Override  
    public Bitmap getBitmap(String url) {  
        return cache.get(url);  
    }  
  
    @Override  
    public void putBitmap(String url, Bitmap bitmap) {  
        cache.put(url, bitmap);  
    
  • 例子
public class MainActivity extends Activity {

    private ImageView mImageView;
    private NetworkImageView mNetworkImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.imageView);
        mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);

        getJSON();
        loadImage();
        NetworkImageView();
        postJSON();
    }
    
    //post jons数据至服务器端
    private void postJSON(){
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String JSONDataUrl = "http://192.168.0.28:3000/postjson";
        Map<String, String> map = new HashMap<String, String>();  
        map.put("key1", "value1");  
        map.put("key2", "value2");  
        JSONObject jsonObject = new JSONObject(map);
        
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST, JSONDataUrl, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("response=" + response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        System.out.println("sorry,Error  " + arg0.getMessage());
                    }
                });
        requestQueue.add(jsonObjectRequest);    
    }
    
    //从服务器get json数据
    private void getJSON() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String JSONDataUrl = "http://192.168.0.28:3000/";
        final ProgressDialog progressDialog = ProgressDialog.show(this,
                "This is title", "...Loading...");

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET, JSONDataUrl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("response=" + response);
                        if (progressDialog.isShowing()
                                && progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        System.out.println("sorry,Error  " + arg0.getMessage());
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    //采用imageloader加载图片资源
    private void loadImage() {
        String imageUrl = "http://192.168.0.28:3000/getimage";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        ImageLoader imageLoader = new ImageLoader(requestQueue,
                new BitmapCache());
        ImageListener listener = ImageLoader.getImageListener(mImageView,
                R.drawable.test, R.drawable.test);
        imageLoader.get(imageUrl, listener, 200, 200);
    }
    
    //采用NetworkImageView组件加载图片资源
    private void NetworkImageView() {
        String imageUrl = "http://192.168.0.28:3000/getimage";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        ImageLoader imageLoader = new ImageLoader(requestQueue,
                new BitmapCache());

        mNetworkImageView.setTag("url");
        mNetworkImageView.setImageUrl(imageUrl, imageLoader);
    }
}

遇到的问题:

1、“Error generating final archive: Found duplicate file for APK: AndroidManifes”,引用的jar包有问题,里面包含了res和manifest.xml,重新在网上下载了一个就OK了;

2、ImageLoader.get()报空指针错误,事实上是findViewById写到setContentView前面去了,导致资源的句柄根本就没取到;这个低级错误,eclipse工程是不会提醒的。。,程序报错要到imageview在获取到网络图片前加载本地默认图片是才报错,折腾了半天啊!!

你可能感兴趣的:(Volley)