如何 post JSON 通过 volley header 请求?

How to post JSON request via volley with header?

我尝试通过 Volley post JSON 请求并收到错误 BasicNetwork.performRequest:意外响应代码 500 这个APIURL在POSTMAN中测试ok,可以被IOS版本的app调用。

我怀疑是 headers 值没有添加到我的代码中(如果我错了请纠正我)

我试图在我的代码中添加 getHeaders() 函数,但似乎不起作用。有人可以告诉我如何解决这个问题吗?

邮递员结果

这是我当前的代码:-

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.clipped_product_page)
        setTitle("Clipped Product")
        val actionbar = supportActionBar

        actionbar?.setDisplayHomeAsUpEnabled(true)
        actionbar?.setDisplayHomeAsUpEnabled(true)

        val URL = "https://api.sample.com/Api/user_id"

        val json = JSONObject()
        json.put("user_id", "1")
        json.put("code", "AB")

        val jsonOblect = object : JsonObjectRequest(
                Request.Method.POST,
                URL,
                json,
                Response.Listener {response ->
                    // Get your json response and convert it to whatever you want.
                    Toast.makeText(this, "You Clicked: $response", Toast.LENGTH_SHORT).show()
                },
                Response.ErrorListener {
                    Toast.makeText(this, "Error $it .message", Toast.LENGTH_SHORT).show()
                }
        ) {
            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val headers = HashMap<String, String>()
                headers.put("Content-Type", "application/json")
                return headers
            }
        }

        VolleySingleton.getInstance(this).addToRequestQueue(jsonOblect)

    }

如何实现这一点并将输出转换为数组列表?

请帮忙谢谢

使用 Volley

发送 post 请求
String URL = "";

JSONObject json = new JSONObject();
json.put("email", "abc@abc.com");
json.put("password", "");
json.put("user_type", "");

 JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, json, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
             // Get your json response and convert it to whatever you want.
        }
    }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
                // Error
          }
    }) {
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
             final Map<String, String> headers = new HashMap<>();
             headers.put("Authorization", "TOKEN");//put your token here
             return headers;
          }
};

VolleyApplication.getInstance().addToRequestQueue(jsonOblect);

最后我使用如下示例代码解决了我的问题:-

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.clipped_product_page)
        setTitle("Clipped Product")
        val actionbar = supportActionBar

        actionbar?.setDisplayHomeAsUpEnabled(true)
        actionbar?.setDisplayHomeAsUpEnabled(true)

        val URL = "https://api.sample.com/Api/user_id"

        val stringRequest = object : StringRequest(Request.Method.POST, URL,
                Response.Listener { response -> Toast.makeText(this, response, Toast.LENGTH_LONG).show() },
                Response.ErrorListener { error -> Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show() }) {
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params["user_id"] = "7"
                params["code"] = "MY"

                return params
            }

        }

        VolleySingleton.getInstance(this).addToRequestQueue(stringRequest)
}

我将 JsonObjectRequest 更改为 StringRequest