Reputation: 769
I made REST API using Django and now I want to connect it to android. I'm using Kotlin and Volley library. I created code and I'm still getting this error:
E/Volley: [287] BasicNetwork.performRequest: Unexpected response code 400 for http://laude.ct8.pl/api/user/login/.
Here is my Kotlin code:
val jsonObj = JSONObject()
val LOGIN_API_URL = "http://laude.ct8.pl/api/user/login/"
loginBtn.setOnClickListener {
jsonObj.put("username", username.text)
jsonObj.put("password", passwd.text)
val que = Volley.newRequestQueue(this@MainActivity)
val req = JsonObjectRequest(Request.Method.POST, LOGIN_API_URL,
Response.Listener {
response ->
Toast.makeText(this@MainActivity, response.toString(), Toast.LENGTH_LONG).show()
},
Response.ErrorListener {
error ->
Toast.makeText(this@MainActivity, error.toString(), Toast.LENGTH_LONG).show()
})
que.add(req)
}
I saw that problem might by with Content-Type
, so I tried with this line, I added it under jsonObj.put("password", passwd.text)
.
Here is this line:
jsonObj.put("Content-Type", "application/json")
.
And here is some test login data:
username: testUser1232
password: test123123
Thanks a lot for help!
Upvotes: 0
Views: 727
Reputation: 1997
When you create the JsonObjectRequest you don't pass the jsonObj
to it.
Try:
val req = JsonObjectRequest(Request.Method.POST, LOGIN_API_URL, jsonObj,
Response.Listener {
response ->
Toast.makeText(this@MainActivity, response.toString(), Toast.LENGTH_LONG).show()
},
Response.ErrorListener {
error ->
Toast.makeText(this@MainActivity, error.toString(), Toast.LENGTH_LONG).show()
})
Upvotes: 1