使用Volley获取服务器数据demo

来源:互联网 发布:mac调整鼠标大小 编辑:程序博客网 时间:2024/05/16 17:04

以前写项目搭框架的时候,从服务器获取数据刚开始自己写相应的异步获取的类,后来再做项目发现用自己写的,不怎么好,开始研究afinal,用finalhttp感觉还可以,用着还行,后来大牛说afinal没有volley用着爽,再说了,volley毕竟是Google出的,所以这次写项目的时候,就试着用了下,下面是我用vooley请求数据的例子,我写的只是vooley的冰山一角哦可怜

(1)这是一个接口,需要使用接口回调,才写的   
public interface VolleyListener {void onComplete(int tag,String json);void onException(int tag,String error);}
(2)自己定义的添加参数的类
public class VolleyParams extends HashMap<String, String>{public void add(String key,String value){this.put(key, value);}}

(3)从服务器请求数据的核心类  
这一步的大致步骤是:
1、创建RequestQueue 对象
2、无论是get请求还是post请求,都需要StringRequest对象
3、实现里面的ErrorListener,Listener接口,并重写里面的方法,如果需要传递参数重写getParams()方法
4、向RequestQueue 对象中添加StringRequest对象  
public class VolleyRequest {public static void request(final int tag,Context context, String method, String url,final VolleyParams params,final VolleyListener listener) {StringRequest request=null;RequestQueue queue=Volley.newRequestQueue(context);if("GET".equals(method)){//get方式请求数据request = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {@Overridepublic void onResponse(String response) {// TODOAuto-generated method stub //// 这里我们可以拿到成功返回的json值用来进一步解析Log.e("ssy", "成功=" + response);listener.onComplete(tag, response);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {// TODOAuto-generated method stub // 这里是返回失败的Log.d("ssy", "失败=" + error);listener.onException(tag, error.toString());}});}else if("POST".equals(method)){//post方式请求数据request = new StringRequest(Request.Method.POST,url,new Response.Listener<String>() {@Overridepublic void onResponse(String response) {// TODOAuto-generated method stub //// 这里我们可以拿到成功返回的json值用来进一步解析Log.e("tcyx", "成功=" + response);listener.onComplete(tag, response);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {// TODOAuto-generated method stub // 这里是返回失败的Log.d("tcyx", "失败=" + error);listener.onException(tag, error.toString());}}) {@Overrideprotected Map<String, String> getParams() throws AuthFailureError {// TODO Auto-generated method stub// org.apache.http.NameValuePair key = new// BasicNameValuePair("key","123456");/** * 这里是post方式需要传递的参数 */return params;}};}else{Log.d("ssy", "传入的请求方式有误");}queue.add(request);}}
(4)这是一个主activity,里面用到接口回调,其他的就没有什么值得说的啦,XML布局很简单,就一个textView自己写吧
public class MainActivity extends Activity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.tv);VolleyParams params=new VolleyParams();VolleyRequest.request(1,                         this, "GET", "http://shishangyuan.coding-coder.cn/ClientApi/user.php?act=user_register&debug=true&api_version=1.0&user_name=15090653904&password=123456&user_rand=78573&next_password=123456", null,new VolleyListener() {@Overridepublic void onException(int tag, String error) {// TODO Auto-generated method stubLog.d("ssy", "onException------->"+error);tv.setText(error);}@Overridepublic void onComplete(int tag, String json) {// TODO Auto-generated method stubLog.d("ssy", "onComplete------->"+json);tv.setText(json);}});}}


6 0
原创粉丝点击