Volley框架的学习总结(1)

1 Volley的主要功能:

  1. 网络请求数据异步下载;
  2. 网络请求的优先级处理
  3. 请求数据的缓存处理;
  4. 多级别取消请求
  5. 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)

步骤:

1.建立请求队列: RequestQueue mQueue= Volley.newRequestQueue(this);

2.请求对象: ImageRequest imageRequest = new ImageRequest()

3.把请求对象添加到请求队列中: mQueue.add(imageRequest);

4.取消请求: mQueue.cancelAll(this);

代码如下:

package com.example.kaiwei.vollydemo1;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {
    //建立请求队列;
    private RequestQueue mQueue;
    private TextView textview;
    private ImageView imageview;
    private final String url_weather = "http://www.weather.com.cn/adat/cityinfo/101010100.html";
    private final String url_image = "http://i.tq121.com.cn/i/wap/logo.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.textview);
        imageview = (ImageView) findViewById(R.id.imageview);
        mQueue = Volley.newRequestQueue(this);
        loadJson();
        loadImageView();
    }
    private void loadImageView() {
        //图片请求;
        ImageRequest imageRequest = new ImageRequest(
                url_image,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        imageview.setImageBitmap(response);
                    }
                }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                imageview.setImageResource(R.drawable.ic_launcher);
            }
        });
        //把请求添加到请求队列中;
        mQueue.add(imageRequest);
    }

    private void loadJson() {
        // TODO Auto-generated method stub
        //json数据请求;
        StringRequest stringRequest = new StringRequest(url_weather,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("response", response);
                        textview.setText(response);
                        System.out.println("response" + response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("response", error.getMessage(), error);
            }
        });
        //把请求添加到请求队列中;
        mQueue.add(stringRequest);
    }
    @Override
    protected void onStop() {
        super.onStop();
        //取消这个队列里的所有请求
        mQueue.cancelAll(this);
    }
}

运行结果:
“weatherinfo”:{“city”:”北京”,”cityid”:”101010100”,”temp1”:”15℃”,”temp2”:”5℃”,”weather”:”多云”,”img1”:”d1.gif”,”img2”:”n1.gif”,”ptime”:”08:00”}}

你可能感兴趣的:(Volley)