Volley的使用

package com.example.cuboo.myvolley;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;
/*
 * Volley is Android Internet Communication Library with faster,more simple and robust。
 * Volley Function:
 * 1、JSON,Pictures(AsyncTask)
 * 2、Sort of The Internet request
 * 3、Solved Priorities of Internet request
 * 4、Cache
 * 5、Cancel request of Levels
 * 6、与Activity的生命周期联动
 *
 * 获取Volley
 */

public class MainActivity extends AppCompatActivity {
    TextView tv;
    ImageView img;
    NetworkImageView net_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        getJSONVolley();
        loadImageVolley();
        loadnetImageVolley();
    }
    private void init(){
        tv = (TextView) findViewById(R.id.tv);
        img = (ImageView) findViewById(R.id.img);
        net_img = (NetworkImageView) findViewById(R.id.net_img);
    }

    //获取JSON数据
    private void getJSONVolley(){
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String JSONDataUrl = "http://www.cnblogs.com/shyy/archive/2012/04/05/2453031.html";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSONDataUrl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        System.out.println("response" + jsonObject);
                        tv.setText("response" + jsonObject);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        System.out.println("Sorry There is a problem");
                        tv.setText("有问题!");
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    private void loadImageVolley(){
        String ImageUrl = "http://s9.sinaimg.cn/middle/002nMbZlzy6NEH8BQRa28&690";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20);
        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s,bitmap);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(img,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
        imageLoader.get(ImageUrl,listener);
    }
    private void loadnetImageVolley(){
        String ImageUrl = "http://image.tianjimedia.com/uploadImages/2012/004/6481LRQE84D2.jpg";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20);
        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s,bitmap);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
        net_img.setTag("url");
        net_img.setImageUrl(ImageUrl,imageLoader);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值