首页 > 代码库 > Android利用Volley异步加载数据完整详细示例(一)

Android利用Volley异步加载数据完整详细示例(一)

MainActivity如下:
package cc.cn;

import java.util.HashMap;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
/**
 * Demo描述:
 * 去年一个项目中简单的使用了Volley,参见:
 * http://blog.csdn.net/lfdfhl/article/details/12223345
 * 最近guolin写了几篇文章详细介绍Volley,所以在此比较系统地学习一下.
 * 在本示例中,主要包括:
 * 1 利用Volley实现Get请求
 * 2 利用Volley实现Post请求
 * 3 利用Volley实现Json数据请求
 * 
 * 注意事项:
 * 1 引入jar包volley.jar
 * 2 网络权限<uses-permission android:name="android.permission.INTERNET"/>
 * 
 * 学习资料:
 * http://blog.csdn.net/guolin_blog/article/details/17482095
 * Thank you very much
 *
 */
public class MainActivity extends Activity {
	private RequestQueue mRequestQueue;
	private StringRequest mStringRequest;
	private Context mContext;
	private JsonObjectRequest mJsonObjectRequest;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        volley_get();
        //volley_post();
        //volley_json();
    }
    

    
    //利用Volley实现Get请求
    private void volley_get(){
    	mContext=this;
    	String url="http://www.baidu.com/";
    	//1 创建RequestQueue对象
    	mRequestQueue=Volley.newRequestQueue(mContext);
    	//2 创建StringRequest对象
		mStringRequest = new StringRequest(
				url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
                        System.out.println("请求结果:"+response);
					}
				}, 
				new Response.ErrorListener() {
					@Override
					public void one rrorResponse(VolleyError error) {
                        System.out.println("请求错误:"+error.toString());
					}
				});
		//3 将StringRequest添加到RequestQueue
		mRequestQueue.add(mStringRequest);
    }
    
    //利用Volley实现Post请求
	private void volley_post() {
		String url = "http://hxyj.cwwic.cn/wap/api_cs/user.php?action=login";
		mContext = this;
		mRequestQueue = Volley.newRequestQueue(mContext);
		mStringRequest = new StringRequest(
				Method.POST, 
				url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						System.out.println("请求结果:" + response);
					}
				}, 
				new Response.ErrorListener() {
					@Override
					public void one rrorResponse(VolleyError error) {
						System.out.println("请求错误:" + error.toString());
					}
				}) {
			//携带参数
			@Override
			protected HashMap<String, String> getParams()throws AuthFailureError {
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("un", "411111222");
				hashMap.put("pw", "180034111");
				return hashMap;
			}

		};
		mRequestQueue.add(mStringRequest);

	}
	
	 //利用Volley实现Json数据请求
	private void volley_json() {
		mContext = this;
		String url = "http://m.weather.com.cn/data/101010100.html";
		// 1 创建RequestQueue对象
		mRequestQueue = Volley.newRequestQueue(mContext);
		// 2 创建JsonObjectRequest对象
		mJsonObjectRequest = new JsonObjectRequest(
				url, 
				null,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						System.out.println("请求结果:" + response.toString());
					}
				}, 
				new Response.ErrorListener() {
					@Override
					public void one rrorResponse(VolleyError error) {
						System.out.println("请求错误:" + error.toString());
					}
				});

		// 3 将JsonObjectRequest添加到RequestQueue
		mRequestQueue.add(mJsonObjectRequest);

	}
    
    
    
}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学习Volley(一)" 
        android:layout_centerInParent="true"
     />

</RelativeLayout>