【问题标题】:Android Volley gives me 400 error code (Bad Request)Android Volley 给了我 400 错误代码(错误请求)
【发布时间】:2016-04-20 17:39:30
【问题描述】:

我正在尝试使用 Volley 向我的应用发出 POST 请求,但它说

E/Volley﹕ [9988] BasicNetwork.performRequest: Unexpected response code 400 for http://test_url/SendMessage

但它在PostMan 中完美运行。

邮差截图:

这是我的代码:

public JSONObject getJsonObject() {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("Recipients", new String[]{authorId + ":I"});
        postParam.put("Subject", etSubject.getText().toString());
        postParam.put("Text", etMessage.getText().toString());
        postParam.put("MessageState", "N");
        postParam.put("ParentId", "");
        postParam.put("PrivateMessageGuid", GUID);
        Log.i("JSON Parameter ===", Utils.getRequestJSONObject(postParam).toString());
        return Utils.getRequestJSONObject(postParam);
    }

JSON 参数 === {"PrivateMessageGuid":"ac74df70-77d8-4281-a723-5c3c65f4d212", "Text":"hello test", "MessageState":"N","Subject":"Re: Hello Sundar","Recipients":["311:I"],"ParentId":""}

主代码:

JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
            Constants.ReplyMessageUrl, getJsonObject(),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(),
                            "Message Send", Toast.LENGTH_SHORT).show();

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", mApp.getTokenType() + " " + mApp.getAccessToken());
            headers.put("Content-Type", "application/json");
            headers.put("Accept", "application/json");
            return headers;
        }
    };

    RequestQueue mRequestQueue = mApp.getRequestQueue();
    mRequestQueue.add(jsonReq);

请帮我解决。谢谢。

【问题讨论】:

标签: android json android-volley postman bad-request


【解决方案1】:

安装wireshark并比较其中的两个请求以查看实时流量的差异。 + 最好使用 GSON 并根据 getJsonObject() 中的内容完成课程

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多