簡體   English   中英

如何通過帶有標題的 volley 發布 JSON 請求?

[英]How to post JSON request via volley with header?

我嘗試通過 Volley 發布 JSON 請求並收到錯誤BasicNetwork.performRequest: Unexpected response code 500 This API URL 在 POSTMAN 中測試正常,可以被 IOS 版本的應用程序調用。

我懷疑標題值沒有添加到我的代碼中(如果我錯了,請糾正我)

我試圖在我的代碼中添加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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM