我想使用POST方法将用户名和密码字符串发布到php页面,但找不到解决方案。因此,我尝试使用JSONRequest方法,但它始终为我提供Response.ErrorListener的结果。请帮助解决。
StringRequest的代码:
UserNamePassword =“名称= aaa&密码= 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)
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)
最佳答案
尝试一下
...
}, Response.ErrorListener { error: VolleyError ->
println("Error $error.message")
}
...
正如您所说,我们收到了以下错误消息
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,
然后我们可以看到问题是另一个问题,例如,您的Web服务上确实发生了错误,并且您没有将它们作为有效json再次发送给应用程序。