如何解决来自服务器的 POST 和响应 null 的问题Kotlin
我在 kotlin 中的 Volley POST 有问题:
当我使用以下代码时,我的应用程序继续“Response.Listener”,但数组为空,因此当我尝试显示我刚刚发送的信息时,我只能得到“空”。>
你们能帮帮我吗? :)
有kotlin代码:
private fun sendHtmlRequest(view: View){
val emailreq = view?.findViewById<EditText>(R.id.editText_email)
val pwdreq = view?.findViewById<EditText>(R.id.editText_password)
val email = emailreq.text.toString()
val pwd = pwdreq.text.toString()
val jsonobj = JSONObject()
var url = "https://www.dorian-roulet.com/testStage-master/mobileApp/testpostone.PHP"
jsonobj.put("mail",email)
jsonobj.put("pwd",pwd)
val que = Volley.newRequestQueue(context)
val req = JsonObjectRequest(Request.Method.POST,url,jsonobj,Response.Listener { response ->
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche $response $jsonobj")
println("Yessai")
},Response.ErrorListener{
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche pas mec $jsonobj")
println("Erreur")
}
)
que.add(req)
}
<?PHP
$reponse = array("mail" => $_POST["mail"],"pwd" => $_POST["pwd"]);
echo json_encode($reponse);
?>
我尝试使用 GET 从 GET 数组中接收此数据,但问题仍然是... 当我查看服务器日志时,我可以看到我的应用程序有一个 POST 请求(即使我看不到请求中的内容)。
我使用此代码执行登录任务,但在 atm 中,我只想从我的应用程序发送发布请求。
请帮忙:)
解决方法
编辑:您的 PHP 代码不好。 要获取 JSON 数据,您不应该使用 $_POST (即使它是通过 post 发送的)。改用这个:
<?php
$data = json_decode(file_get_contents('php://input'),true);
$reponse = array("mail" => $data["mail"],"pwd" => $data["pwd"]);
echo json_encode($reponse);
?>
这是一个适用于 volley 的代码(它与您拥有的没有太大区别:-)):
val jsonObjectRequest: JsonObjectRequest = object : JsonObjectRequest(Method.POST,url,**jsonobj**,Response.Listener { response: JSONObject ->
try {
val email = response.getString("email")
val password = response.getString("password")
} catch (e: JSONException) {
// catch/handle JSON error
} catch (e: Exception) {
// catch/handle other error
}
},Response.ErrorListener { error: VolleyError ->
//Error Listener code
}) {
override fun getBodyContentType(): String {
return "application/x-www-form-urlencoded"
}
// you can override more functions here if needed (getHeader,etc.)
}
queue.add(jsonObjectRequest)
对于您的情况,它可能会给出:
private fun sendHtmlRequest(view: View){
val emailreq = view?.findViewById<EditText>(R.id.editText_email)
val pwdreq = view?.findViewById<EditText>(R.id.editText_password)
val email = emailreq.text.toString()
val pwd = pwdreq.text.toString()
val jsonobj = JSONObject()
var url = "https://www.dorian-roulet.com/testStage-master/mobileApp/testpostone.php"
jsonobj.put("mail",email)
jsonobj.put("pwd",pwd)
val que = Volley.newRequestQueue(context)
val req = JsonObjectRequest(Request.Method.POST,jsonobj,Response.Listener { response: JSONObject ->
val mailBack = response.getString("mail")
val pwdBack = response.getString("pwd")
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche $response $mailBack - $pwdBack")
println("Yessai")
},Response.ErrorListener{
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche pas mec $jsonobj")
println("Erreur")
}
)
que.add(req)
}
,
你把我必须放在里面的代码发给我后,我就有了代码: (仍然返回空值)
private fun sendHtmlRequest(view: View){
val emailreq = view?.findViewById<EditText>(R.id.editText_email)
val pwdreq = view?.findViewById<EditText>(R.id.editText_password)
val email = emailreq.text.toString()
val pwd = pwdreq.text.toString()
val jsonobj = JSONObject()
var url = "https://www.dorian-roulet.com/testStage-master/mobileApp/testpostone.php"
jsonobj.put("mail",pwd)
val que = Volley.newRequestQueue(context)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.POST,Response.Listener { response: JSONObject ->
val emails = response.getString("mail")
val passwords = response.getString("pwd")
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche $emails $passwords $response $jsonobj")
},Response.ErrorListener {
view.findViewById<TextView>(R.id.error_login)?.text = ("Ca marche pas mec $jsonobj")
println("Erreur")
}) /*{
override fun getBodyContentType(): String {
return "application/x-www-form-urlencoded"
}
// you can override more functions here if needed (getHeader,etc.)
}*/
que.add(jsonObjectRequest)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。