0

I made REST API with Django Rest Framework. Currently, I'm working on a mobile app for my site. When I do POST request (it's my second POST request in my app) I'm getting this error:

E/Volley: [385] BasicNetwork.performRequest: Unexpected response code 500 for http://laude.ct8.pl/api/user/data/aktualizacja/

Here is my code:

private val UPDATE_URL = "http://laude.ct8.pl/api/user/data/aktualizacja/"
private var volleyRequest: RequestQueue? = null
private val jsonObj = JSONObject()

volleyRequest = Volley.newRequestQueue(this)

private fun aktualizacja() {

        jsonObj.put("librus_user", usernameEdit.text)
        jsonObj.put("librus_pswd", passwordEdit.text)

        val req = JsonObjectRequest(Request.Method.POST, UPDATE_URL, jsonObj,
                Response.Listener { response ->
                    Toast.makeText(this, response.toString(), Toast.LENGTH_LONG).show()
                    val user = Intent(this, UserActivity::class.java)
                    startActivity(user)
                },
                Response.ErrorListener { error ->
                    Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
                })
        req.retryPolicy = DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)

        volleyRequest!!.add(req)
    }

When I use same data on my website I'm not getting any errors, I got it only on a mobile app. Thanks for help !

xaos_xv
  • 759
  • 1
  • 10
  • 27
  • 500 means there's an internal server error. This isn't an Android issue, this is a server/website issue – Zoe Nov 15 '17 at 20:21
  • Error 500 is the Server error that means there is no problem with your code (there may be an error) but you have to check your server error cause before checking your app code! Show logs in your server! – Xenolion Nov 15 '17 at 20:21
  • @Xenolion I have only access to error and access logs. I'm not getting any of that. – xaos_xv Nov 16 '17 at 15:00
  • After a careful observation of the site above I have figured what might be the cause of the error above! Have you already solved it or should I still answer it! – Xenolion Nov 16 '17 at 21:57

1 Answers1

0

The server error is caused by you making JsonObjectRequest and passing an JsonObject as a body of parameter while your server requires them as Strings(I observed the link!). The quick fix is to use StringRequest and pass your parameters as a String as ! Check the code below to get the idea!

private val UPDATE_URL = "http://laude.ct8.pl/api/user/data/aktualizacja/"
private var volleyRequest: RequestQueue? = null

volleyRequest = Volley.newRequestQueue(this)

private fun aktualizacja() {

        val req = object : StringRequest(Request.Method.POST,
                UPDATE_URL,
                Response.Listener { response ->
                Toast.makeText(this, response, Toast.LENGTH_LONG).show()
                val user = Intent(this, UserActivity::class.java)
                startActivity(user)

                }, Response.ErrorListener { e ->

               Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
        }) {
            public override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("username", usernameEdit.text)
                params.put("password", passwordEdit.text)
                return params
            }
        }
        req.retryPolicy = DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)

    volleyRequest!!.add(req)

}

You should also NOTE that the response is also a String so if you need it as a JSONOject convert it.

Sorry if you may get any errors in the code! I wrote the code in Java and convert to Kotlin using Android Studio!! I hope you will get the general idea!

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • Thanks for the answer, but I'm getting the same error. – xaos_xv Nov 17 '17 at 15:14
  • Okay but I have realised that even visiting the link using the browser it crashes leading to an exception so you have 500 code which is the server error. Please post using the browser first trying to debug and all will go well! – Xenolion Nov 18 '17 at 06:25
  • This is the error `During handling of the above exception (list assignment index out of range), another exception occurred:` – Xenolion Nov 18 '17 at 06:27
  • Yeah, this error shows up when you put incorrect data. I shouldn't get this error because I use correct data. – xaos_xv Nov 18 '17 at 09:18
  • Can you share with me the correct data or is it classified? – Xenolion Nov 18 '17 at 09:19
  • Sorry but I can't. This is data for my school. – xaos_xv Nov 18 '17 at 09:35
  • Okay then make some fake data (username and password) and allow me to debug for you! – Xenolion Nov 18 '17 at 09:46
  • Done. First, you need to login to this url: `http://laude.ct8.pl/api/user/login/` with this data: username: `fakeUser` and password: `fakeUser`. Then you can go to this url: `http://laude.ct8.pl/api/user/data/aktualizacja/` and use this data: username: `fake` password: `fakePassword` – xaos_xv Nov 18 '17 at 09:58
  • Can I answer the question using java? – Xenolion Nov 18 '17 at 10:02
  • Yeah, no problem. – xaos_xv Nov 18 '17 at 10:05
  • Is the names `librus_user` and `librus_pswd` for password?? Thats what I see from the form! But we used `username` and `password` for it! – Xenolion Nov 18 '17 at 10:43
  • Yeah, I changed it yesterday. I will edit my code in my question. – xaos_xv Nov 18 '17 at 10:49
  • I have finished the request the site needs a person to be logged in first before making the second request! So I am getting `AuthFailerError` I can not make both requests! – Xenolion Nov 18 '17 at 12:46
  • Yeah, I was getting the same error. Is it way to avoid it? – xaos_xv Nov 18 '17 at 13:35
  • Oooh your question becomes good now and you need to find a way to store a session using a cookie or something. I will show you the way here https://stackoverflow.com/questions/16680701/using-cookies-with-android-volley-library – Xenolion Nov 18 '17 at 21:47
  • I added this code `CookieHandler.setDefault(CookieManager())` into my login actvity and I'm still getting same error. – xaos_xv Nov 19 '17 at 08:53