【问题标题】:How to get rid of JSONException: No value for success?如何摆脱 JSONException:没有成功的价值?
【发布时间】:2020-04-19 05:29:46
【问题描述】:

我不知道我做错了什么。我制作了一个注册表单,其中包含一个唯一的代码,我保存在 sql 数据库中,所以当用户输入错误的代码时,我希望出现错误消息或 toast。 现在我面临 org.json JSONException: No value for success.

如何在 mainActivity 中定义的代码 textview 中设置错误消息? 或者我怎样才能让吐司出现。

有没有比我目前使用的更好的方法来限制用户注册(手动将唯一代码保存在数据库中)?

注册.php

<?php

if ($_SERVER["REQUEST_METHOD"] =="POST"){

    require_once 'connect.php';

    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $code=$_POST["code"];

    //for checking if the email address already exists
    $mysql_select="SELECT email FROM register_table where email ='$email' ";

    $res=mysqli_query($conn,$mysql_select);

    if (mysqli_num_rows($res) > 0 ) {
        $row = mysqli_fetch_assoc($res);

        if($email==$row['email']){
            echo "Email already exists";
        }
    }else {
       $code_query="SELECT code FROM code_table where code='$code' ";
       $code_result=mysqli_query($conn,$code_query);
       $roww = mysqli_fetch_assoc($code_result);

       $sql="INSERT INTO register_table (name, email,password) VALUES ('$name', '$email', '$password')";

       if($code!=$roww['code']){

           $resultt["codes"] = "0";
           echo json_encode($resultt);

           $result["success"] = "0";
           echo json_encode($result);

           mysqli_close($conn);

       }else if (mysqli_query($conn, $sql) && $code==$roww['code'] ){

        $resultt["codes"] = "1";
        echo json_encode($resultt);

        $result["success"] = "1";
        echo json_encode($result);

        mysqli_close($conn);

    }else {
       $result["success"] = "0";
       $result["message"] = "error";

       echo json_encode($result);
       echo "registration not successful";
       mysqli_close($conn);
   }
}

}else{
    echo "register method is not post";
}

?>

其余代码完美运行,下面的方法属于AsyncTask类

BackgroundTask.java

 protected void onPostExecute(String res) {

        try {
            JSONObject jsonobject = new JSONObject(res);
            String success = jsonobject.getString("success");
            String code_result=jsonobject.getString("codes");

            if(code_result.equals("0") && success.equals("0")){
                Toast.makeText(context,"Please enter the correct code",Toast.LENGTH_SHORT).show();
            }else if (success.equals("1") && code_result.equals("1")) {
                Toast.makeText(context, "Register Successfully!", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(context, "Register Error!" + e.toString(), Toast.LENGTH_SHORT).show();
        }
}

【问题讨论】:

标签: java php android mysql json


【解决方案1】:

问题是你多次使用echo,你也应该只对json编码数据使用echo..

要解决问题,只需尝试使用 postman 获取不同场景的 API 响应,您就会明白问题所在..

【讨论】:

    【解决方案2】:

    JSONObject.getString() 方法在对象中未找到输入键时不返回 null,并会抛出 JSONException。

    使用optString()

    String success = jsonobject.optString("success");
    String code_result = jsonobject.optString("codes");
    if (success != null && code_result != null) {
          // ...
    }
    

    【讨论】:

    • 您能详细说明什么不起作用吗?你仍然得到相同的 JSONException?
    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2011-09-15
    • 1970-01-01
    相关资源
    最近更新 更多