Volley(三)--实例化使用

2019-01-13  本文已影响0人  azu_test

使用前的准备

    public static final String BAI_DU = "https://www.baidu.com";
    public static final String TAO_BAO = "http://api.k780.com:88/?app=phone.get&phone=13800138000&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
    public static final String IMAGE_URL_1 = "https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/trm5VMeFp9mJejJH2asZZT0ML63erOW3QAMSkjEMsLwByykbJwsHj7QmPbQDDUU43BJpHTXxyiaY24LXlA6zKDQ/640?wx_fmt=jpeg";
        queue = Volley.newRequestQueue(mContext);

1. StringRequest的使用

        StringRequest mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                textView.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                textView.setText(volleyError.getMessage());
            }
        });
        queue.add(mStringRequest);

2. JsonRequest的使用

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JsonModel jsonModel = new Gson().fromJson(response.toString(),JsonModel.class);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                if (volleyError!=null){
                    ShowLogUtil.addLog("wuyazhouHttp",volleyError.getMessage().toString());
                }else {
                    ShowLogUtil.addLog("wuyazhouHttp","不知道啊");
                }
            }

        });
        queue.add(jsonObjectRequest);

其中JsonModel源码如下

public class JsonModel {
    public int success;
    public Result result;
    
    class Result{
        public String status;
        public String phone;
        public String area;
        @SerializedName("postno")
        public String postNum;
        public String att;
        @SerializedName("ctype")
        public String type;
        public String par;
        public String prefix;
        public String operators;
        @SerializedName("style_simcall")
        public String styleSimCall;
        @SerializedName("style_citynm")
        public String styleCityName;
    }
}

3. ImageRequest加载图片

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.RGB_565,new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        queue.add(imageRequest);

3. ImageLoader加载图片

       ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }
            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });

        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView,R.drawable.saierda,R.drawable.saierda);
        imageLoader.get(url,imageListener);

3. NetworkImageView加载图片

ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });
        networkImageView.setDefaultImageResId(R.drawable.saierda);
        networkImageView.setErrorImageResId(R.drawable.saierda);
        networkImageView.setImageUrl(url,imageLoader);



Volley--网络通讯框架 目录

上一篇下一篇

猜你喜欢

热点阅读