Volley框架学习

---恢复内容开始---

Volley优缺点:除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作比如说下载文件等,Volley的表现就会非常糟糕。

在对服务器进行请求的时候有三种对象可以选择:

StringRequest 这种情况的应用场景是不知道是下面哪个对象传进来的时候用。

JsonObjectRequest 确定是JsonObjectRequest 使用

JsonArrayRequest 确定是JsonArrayRequest 使用

---------------------------------------------------------

使用Volley的步骤

1. 创建一个RequestQueue对象。

package com.example.voll;

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

import android.app.Application;

public class MyApplication extends Application {
    // 建立队列
    private static RequestQueue queue;

    // 程序主入口
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        queue = Volley.newRequestQueue(getApplicationContext());
    }

    // 静态方法返回队列
    public static RequestQueue getHttpQueue() {
        return queue;
    }
}

然后在清单文件进行配置name

    <application
        android:name="com.example.voll.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

2. 创建一个StringRequest对象。

3. 将StringRequest对象添加到RequestQueue里面。

// 添加到tag和队列
        request.setTag("getjsonMethod");
        MyApplication.getHttpQueue().add(request);

--------------------------------------------------------------------------------------------------------------------------------------------------------

我们来看第二步的方式,

StringRequest 的get请求

String url = "http://192.168.0.103:8080/json/servlet/JsonAction?flag=listmap";
        StringRequest request = new StringRequest(Method.GET, url,
        // 请求成功调用
                new Listener<String>() {
                    @Override
                    public void onResponse(String arg0) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, arg0, 0).show();
                    }
                },
                // 请求失败调用
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        Toast.makeText(MainActivity.this,
                                "错误信息" + arg0.toString(), 0).show();

                    }
                });
        // 添加到tag和队列
        request.setTag("getMethod");
        MyApplication.getHttpQueue().add(request);

StringRequest 的post请求

    private void volleyStringPost() {
        String url = "http://192.168.0.110:8080/json/servlet/JsonAction";
        StringRequest stringPost = new StringRequest(Method.POST, url,
                new Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        Toast.makeText(MainActivity.this, arg0, 0).show();

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        Toast.makeText(MainActivity.this, arg0.toString(), 0)
                                .show();

                    }
                }) {
            // 因为post请求不能在url里直接写出,所以这里要写这个方法来获取参数
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                // TODO Auto-generated method stub
                Map<String, String> map = new HashMap<String, String>();
                map.put("flag", "listmap");
                return map;
            }
        };
        stringPost.setTag("StringPost");
        MyApplication.getHttpQueue().add(stringPost);

    }

JsonObjectRequest get请求

    private void volleyJsonGet() {
        String url = "http://192.168.0.103:8080/json/servlet/JsonAction?flag=listmap";
        JsonObjectRequest request = new JsonObjectRequest(Method.GET, url,
                null, new Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject arg0) {
                        // TODO Auto-generated method stub
                        try {
                            JSONArray jsonArray = arg0.getJSONArray("listmap");
                            Toast.makeText(MainActivity.this,
                                    jsonArray.get(0).toString(), 0).show();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        // TODO Auto-generated method stub

                    }
                });
        // 添加到tag和队列
        request.setTag("getjsonMethod");
        MyApplication.getHttpQueue().add(request);
    }

JsonObjectRequest post请求

 

posted @ 2016-01-13 13:27  aaddrrooiidd  阅读(156)  评论(1编辑  收藏  举报