且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误使用凌空库将数据发送到服务器

更新时间:2023-01-17 11:25:13

,你应该使用 getBody 你的POST请求。

您可以参考我下面的工作示例code(取代我的的JSONObject URL由你和)。希望这有助于!

You can refer to my following working sample code (replace my JSONObject and Url by yours). Hope this helps!

        ...   
        try {
            RequestQueue queue = Volley.newRequestQueue(this);
            jsonBody = new JSONObject();
            jsonBody.put("Title", "Android Volley POST DATA Demo");
            jsonBody.put("Author", "BNK");
            jsonBody.put("Date", "2015/09/17");
            requestBody = jsonBody.toString();

            StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // do something...
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // do something...
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return requestBody == null ? null : requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        e.printStackTrace();
                        return null;
                    }
                }
            };
            queue.addToRequestQueue(stringRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ...