Volley请求框架(demo)

Volley请求框架(demo)
Volley是 Google 推出的 Android 异步网络请求框架和图片加载框架。下面通过代码来了解volley的请求功能。
demo的下載地址:Volleydemo
首先就是引入volley的jar包,然后我们自己创建一个类MyApplication
package com.example.volleydemo1;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import android.app.Application;

public class MyApplication extends Application {

	public static RequestQueue queues;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		queues = Volley.newRequestQueue(getApplicationContext());
	}

	public static RequestQueue getHttpQueues() {
		return queues;

	}
}

并且在清单文件中声明:
 android:name="com.example.volleydemo1.MyApplication"

下一步:
package com.example.volleydemo1;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;

public class VolleyRequest {
	public static StringRequest stringRequest;
	public Context context;

	static class MyStringRequest extends StringRequest {
		public Map headers = new HashMap();

		public MyStringRequest(int method, String url,
				Listener listener, ErrorListener errorListener) {
			super(method, url, listener, errorListener);
			// TODO Auto-generated constructor stub
		}
  //有的服务器要key,所有添加了一个key方法
		@Override
		public Map getHeaders() throws AuthFailureError {
			// TODO Auto-generated method stub
			return headers;
		}

	}

}

最后开始请求数据:
package com.example.volleydemo1;

import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.example.volleydemo1.VolleyRequest.MyStringRequest;

import com.google.gson.Gson;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final String TAG = "Volley";
	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		findViewById(R.id.bt_button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String url = "http://192.168.2.132/kpApp/news/picture";
				MyStringRequest myStringRequest = new MyStringRequest(
						Request.Method.GET, url, new Listener() {

							@Override
							public void onResponse(String response) {
								// TODO Auto-generated method stub
								//在這解析數據
								tv.setText(response);
								System.out.println(response);
							}
						}, new ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError error) {
								Log.i("error>", error.toString());

							}
						});
//				myStringRequest.headers.put("apikey",
//						"6ee217488609aa62972663f6eefe6644");
				MyApplication.getHttpQueues().add(myStringRequest);
			}
		});
	}
//	public void processData(String response){
//		Gson gson = new Gson();
//		NewsTopBean newsTopBean = gson.fromJson(response.toString(), NewsTopBean.class);
//		
//	}

}


最後別忘了添加上網權限:
















你可能感兴趣的:(Andorid干货)