【问题标题】:Android Kotlin Volley Post String with multiple variable具有多个变量的 Android Kotlin Volley Post String
【发布时间】:2018-06-07 12:13:00
【问题描述】:

我想使用 POST 方法将一串用户名和密码发布到 php 页面,但我找不到解决方案。因此,我尝试使用 JSONRequest 方法,但它总是给我 Response.ErrorListener 的结果。请帮忙解决。

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)

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)

【问题讨论】:

  • 您在浏览器中得到响应了吗?
  • @Redman 是的,仅适用于 stringRequest 方法。但是对于 JsonRequest 方法,它总是转到 Response.ErrorListener。

标签: php android post kotlin android-volley


【解决方案1】:

试试看

...
}, 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, 

然后我们可以看到问题是另一个问题,例如,您的网络服务确实发生了错误,并且您没有再次将它们作为有效的 json 发送给应用程序。

【讨论】:

  • 谢谢,php 页面假设接收一些 POST 变量。我通过发布一个带有多个变量的字符串来迅速做到这一点。但我是安卓新手。你能告诉我如何在 Kotlin Android Volley 中做到这一点吗?
  • 如果你需要一个 POST 你已经做对了。检查您的网络服务以获取更多详细信息。也许在您的网络服务上发生了导致您的请求崩溃的事情
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多