【问题标题】:Error message when compile and it may be something is wrong in my .php file编译时出现错误消息,我的 .php 文件可能有问题
【发布时间】:2017-02-08 13:55:05
【问题描述】:
org.json.JSONException: No value for Success
org.json.JSONException: No value for Success
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at org.json.JSONObject.getBoolean(JSONObject.java:410)

我收到此错误,它不会从我的数据库中获取数据。

这是我的 php 文件

<?php
    $con = mysqli_connect("", "",     "", "");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);

$response = array();
$response["success"] = false;  

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["name"] = $name;
    $response["age"] = $age;
    $response["username"] = $username;
    $response["password"] = $password;
}

echo json_encode($response);?> 

我无法弄清楚它有什么问题。我已经尝试了很多东西。

这是有错误的代码块

  Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("Success");

                        if (success) {
                            String name = jsonResponse.getString("name");
                            int age = jsonResponse.getInt("age");

【问题讨论】:

  • 我认为这是错误的代码块。您是否正在尝试将响应转换为 json 对象?错误说例如at org.json.JSONObject.getBoolean(JSONObject.java:410)
  • json 区分大小写 ...
  • 对不起。我对此很陌生。它不会在代码中显示任何错误。我读了很多帖子说我的 php 文件中可能有错误。

标签: java php android sql


【解决方案1】:

JSON 区分大小写。

您正在返回“成功”的值,即,

$response["success"] = true;

但试图获取“成功”的值,即,

boolean success = jsonResponse.getBoolean("Success");

这就是它导致错误的原因

org.json.JSONException: No value for Success

所以在两个代码中使用相同的大小写

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多