码迷,mamicode.com
首页 > Web开发 > 详细

Volley--http框架的实例

时间:2015-07-10 16:41:31      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:volley   http框架   

1.想要连接,首先要生成队列。利用application生成

public class MyApplication extends Application {
    public static RequestQueue queue;

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

    public static RequestQueue getHttpQueue() {
        return queue;
    }
}

post方法和get方法的封装,实现的方法有兴趣的同学就研究研究,只是想用一下的同学在onresponse方法传回的arg0就是放回的json格式数据,拿过来解析就行了。
url可以多试几个玩玩。

    private void volley_Post() {
        String url = "http://apis.juhe.cn/mobile/get?";
        StringRequest request = new StringRequest(Method.POST, url,
                new Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        Toast.makeText(MainActivity.this, arg0,
                                Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        Toast.makeText(MainActivity.this, "失败",
                                Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("phone", "13666666666");
                map.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
                return map;
            }
        };
        request.setTag("abcPost");
        MyApplication.getHttpQueue().add(request);
    }

    private void volley_Get() {
//      String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d";

        StringRequest request = new StringRequest(Method.GET, url,
                new Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
//                      Toast.makeText(MainActivity.this, arg0,
//                              Toast.LENGTH_LONG).show();
                        Log.i("info", arg0);
                    }

                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError arg0) {
//                      Toast.makeText(MainActivity.this, "失败",
//                              Toast.LENGTH_LONG).show();
                        Log.i("info", arg0+"");
                    }
                });
        request.setTag("abcGet");
        MyApplication.getHttpQueue().add(request);
    }

记得要导入volley包

版权声明:本文为博主原创文章,未经博主允许不得转载。

Volley--http框架的实例

标签:volley   http框架   

原文地址:http://blog.csdn.net/a4384142/article/details/46831445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!