来自服务器的 POST 和响应 null 的问题(Kotlin)

时间:2021-06-03 07:36:42

标签: kotlin post request android-volley

我在 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 代码:

<?php
$reponse = array("mail" => $_POST["mail"], "pwd" => $_POST["pwd"]);
echo json_encode($reponse);
?>

我尝试使用 GET 从 GET 数组中接收此数据,但问题仍然是... 当我查看服务器日志时,我可以看到我的应用程序有一个 POST 请求(即使我看不到请求中的内容)。

我使用此代码执行登录任务,但在 atm 中,我只想从我的应用程序发送发布请求。

请帮忙:)

2 个答案:

答案 0 :(得分:0)

编辑:您的 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, url, 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)
}

答案 1 :(得分:0)

你把我必须放在里面的代码发给我后,我就有了代码: (仍然返回空值)

        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 jsonObjectRequest = JsonObjectRequest(
        Request.Method.POST, url, jsonobj,
        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)
}
相关问题