【问题标题】:How to wrap JsonObjectRequest and Volley to be simpler to use?如何包装 JsonObjectRequest 和 Volley 以更简单地使用?
【发布时间】:2015-04-15 09:14:41
【问题描述】:

我想向 API 发送请求并在 Android 中获得 JSON 格式的响应。我使用 Volley 来帮助我。我打算将 JsonObjectRequest 包装在 VolleyHelper 中,以使我更容易使用它。问题是 JsonObjectRequest 的 onResponse() 是无效的,因此我无法返回 JSON 对象。我的想法是让我的 api 调用像这样简单。

JSONObject response = VolleyHelper.getInstance(this).get(url);

JSONObject response = VolleyHelper.getInstance(this).post(url, params);

下面是我的帮助代码,使用 Google 建议的单例模式。

VolleyHelper

public class VolleyHelper {
    private static VolleyHelper mInstance;
    private RequestQueue mRequestQueue;
    private static Context mCtx;

    public static synchronized VolleyHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new VolleyHelper(context);
        }
        return mInstance;
    }

    private VolleyHelper(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
    }

    private RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    private <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public JSONObject get(String url) {

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                //return jsonObject;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //return jsonerror
            }
        });

        addToRequestQueue(request);
    }

    public JSONObject post(String url, Map<String, String> params) {

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                //return jsonObject;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //return jsonerror
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };

        addToRequestQueue(request);
    }
}

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    我认为从异步操作同步返回任何内容不是一个好主意。 android 中的整个异步层——无论是普通的AsyncTask 还是Volley——都是用另一种模式构建的——>callback。在这种情况下,回调是在完成“长时间运行”的网络任务时调用的东西。

    所以,你应该这样设计你的助手类:

    public interface OnFinishListener { public void onFinish( JSONObject o ); }
    
    public class VolleyHelper {
    
        public JSONObject get(String url, OnFinishListener ofl ) {
    
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    ofl.onFinish( jsonObject );
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //return jsonerror
                }
            });
    
            addToRequestQueue(request);
        }
    }
    

    然后:

    OnFinishListener ofl = new OnFinishListener(){
      @Override public void onFinish( JSONObject o ){
        doSomethingOnJson( o );
      }
    }
    JSONObject response = VolleyHelper.getInstance(this).get(url, ofl );
    
    JSONObject response = VolleyHelper.getInstance(this).post(url, params, ofl );
    

    【讨论】:

    • 你的意思是 VolleyHelper.getInstance(this).get(url, ofl ); 没有 JSONObject response = 因为它应该什么都不返回?
    • 是的,不是get,因为它正在阻塞并且阻塞主线程是你最不想做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2019-01-26
    • 2013-05-22
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多