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

如何解决来自服务器的 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 代码

<?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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?