【问题标题】:Does Volley have JSON Argument, String response native supportVolley 是否有 JSON 参数、字符串响应原生支持
【发布时间】:2016-07-29 05:25:22
【问题描述】:

我想发出一个服务器 POST 请求,发送一个 JSON 有效负载,并接收一个字符串响应作为回报。我到处寻找,似乎没有一种简单的方法可以使用 Volley 库来做到这一点,而无需实现自定义请求(在尝试实现它时我完全迷失了)。执行此 POST 请求的“正确”方式是什么?

【问题讨论】:

    标签: android json post android-volley


    【解决方案1】:
        final JSONObject body = new JSONObject();
        try {
            // Todo: populate JSON body
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://...",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
    
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                    }
                }
        ) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return body.toString().getBytes();
            }
    
            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };
    
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    

    【讨论】:

    • 完美 - 感谢您的帮助。我知道某处一定有一个优雅的解决方案......
    【解决方案2】:

    Volley 有 JsonObjectRequest 和 JsonArrayRequest。

     /**
         * Making json object request
         * */
        public void makeJsonObjReq(String url) {
    
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                    url,
                    new Response.Listener<JSONObject>() {
    
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
    //                      msgResponse.setText(response.toString());
    
                        }
                    }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
    
                }
            }) {
    
                /**
                 * Passing some request headers
                 * */
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json");
                    return headers;
                }
    
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("name", "android@android.com");
                    return params;
                }
    
            };
    
            // Adding request to request queue
            ApplicationController.getInstance().addToRequestQueue(jsonObjReq,
                    tag_json_obj);
    
            // Cancelling request
            // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
        }
    
    
    
     /**
         * Making json array request
         * */
        public void makeJsonArryReq(String url, final int _type) {
    
            JsonArrayRequest req = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.d(TAG, response.toString());
                            apiTaskListener.onTaskComplete("error",_type);
    
    
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    apiTaskListener.onTaskComplete("error",_type);
    
                }
            });
    
            // Adding request to request queue
            ApplicationController.getInstance().addToRequestQueue(req,
                    tag_json_arry);
    
            // Cancelling request
            // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
        }
    

    【讨论】:

    • 当然,但我不想“检索”一个 JSON 对象,我想“发布”一个 JSON 对象,作为回报,服务器会向我发送一个响应,以解释为细绳。我是第一个使用 JSONObject 的,服务​​器有一个字符串,我们要交换。
    • 您可以使用 StringRequest strReq = new StringRequest 因为 json 只不过是一种字符串格式。所以你可以将你的json作为字符串发送。 protected Map getParams() throws AuthFailureError { Map params = new HashMap(); params.put("数据",json.toString());返回参数; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多