Android Kotlin Volley Post String with multiple variable


Question

I would like to use POST method to post a string of username and password to a php page but I couldn't find the solution. Thus, i try to use JSONRequest method, but it always gave me the result of Response.ErrorListener. Please help to solve it.

Code for StringRequest:

UserNamePassword = "Name=aaa&Password=bbb"

val queue = Volley.newRequestQueue(this)

val stringRequest = StringRequest(Request.Method.POST, url,
        Response.Listener<String> { response ->
        // Display the first 500 characters of the response string.
           println(response.toString())
        }, Response.ErrorListener { println("That didn't work!") })

// Add the request to the RequestQueue.
queue.add(stringRequest)

Code for JSONRequest:

val jsonobj = JSONObject()

jsonobj.put("Name", "aaa")
jsonobj.put("Password", "bbb")

val que = Volley.newRequestQueue(this)
val req = JsonObjectRequest(Request.Method.POST,url,jsonobj,
            Response.Listener {
                response ->
                //println(response["msg"].toString())
                println("oooooooooooooookkkkkkkkkkkkkkkkkk")
            }, Response.ErrorListener {
                println("Error rrrrrrrrrrrrrrr")
            }
        )
que.add(req)

Result from Android Studio

1
0
6/7/2018 1:03:29 PM

Accepted Answer

Trying it

...
}, Response.ErrorListener { error: VolleyError ->
    println("Error $error.message")
}
...

we have the follow error message as you said us

06-07 20:46:17.317 10064-10064/com.gph.radiobutton I/Choreographer: Skipped 47 frames!  The application may be doing too much work on its main thread.
06-07 20:46:17.320 10064-10064/com.gph.radiobutton I/System.out: Error com.android.volley.ParseError: org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONObject.message
06-07 20:46:17.776 10064-10089/com.gph.radiobutton I/OpenGLRenderer: Davey! duration=1246ms; Flags=0, IntendedVsync=25096758012019, Vsync=25097541345321, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=25097546076936, AnimationStart=25097546158936, PerformTraversalsStart=25097546649936, DrawStart=25097546898936, SyncQueued=25097546932936, SyncStart=25097546989936, IssueDrawCommandsStart=25097547040936, SwapBuffers=25097910009936, FrameCompleted=25098004716936, DequeueBufferDuration=10218000, QueueBufferDuration=5455000, 

Then we can see that the problem is another, e.g., an error do occur on your web service and you don't send them as a valid json to the application again.

1
6/7/2018 1:02:35 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon