Volley中的API问题

时间:2019-05-11 15:20:50

标签: android android-volley postman

我能够向邮递员发出成功的请求,但无法使用android studio中的凌空抽空成功地请求

以下是邮递员请求正文和标头的屏幕截图 enter image description here

enter image description here

以下是我的排球代码

val API = "https://carenextion.org/api/v1/care_team"
val requestQueue = Volley.newRequestQueue(this@ActivityAddCareTeam)
        val stringRequest = object : StringRequest(
            Request.Method.POST,
            API,
            Response.Listener { response ->
                val json = JSONObject(response)
                Log.i("response", response)
                if (json.getBoolean("success")) {
                    Toast.makeText(this@ActivityAddCareTeam, "Care team added", Toast.LENGTH_SHORT).show()
                    finish()
                } else {
                    Toast.makeText(
                        this@ActivityAddCareTeam, "There was an error", Toast.LENGTH_LONG
                    ).show()
                }
            },
            Response.ErrorListener { error ->
                Toast.makeText(this@ActivityAddCareTeam, "There was an error", Toast.LENGTH_LONG).show()
            }) {

            override fun getParams(): MutableMap<String, String> {
                val params = HashMap<String, String>()
                params["title"] = teamName
                params["description"] = teamDescription
                params["gender"] = gender
                params["location"] = teamLocation
                params["age_range"] = ageRange
                return params
            }

            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers["Authorization"] = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0MDA2LCJleHAiOjE1NTc2NzMyNTd9.4y87SG57cbjgh6oo8VQizZsOml-QupE4n4ywD0EUxT4"
                return headers
            }
        }
        requestQueue.add(stringRequest)

我在邮递员中得到了以下回应

{
"success": true,
"user": {
    "id": 342,
    "title": "demo",
    "gender": "male",
    "description": "test desc",
    "age_range": "55-64"
},
"message": "CareTeam Added"

}

但在齐射中,我得到关注

{"success":false,"error":{"code":999,"message":"undefined method `[]=' for nil:NilClass"}}

1 个答案:

答案 0 :(得分:2)

getParams()使用以下代码

override fun getParams(): MutableMap<String, String> {
            val params = HashMap<String, String>()
            params["care_team[title]"] = teamName
            params["care_team[description]"] = teamDescription
            params["care_team[gender]"] = gender
            params["care_team[location]"] = teamLocation
            params["care_team[age_range]"] = ageRange
            return params
        }