JSONObject返回null

时间:2017-01-29 15:45:31

标签: php json android-studio

我正在尝试从我的数据库中检索一个增值号码,以便在用户登录时显示。它工作正常,然后突然停止工作并开始返回错误:

  

org.json.JSONException:类型为vat时的值为null   org.json.JSONObject $ 1无法转换为int

我不确定为什么。任何帮助将不胜感激。

以下是代码:

                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success =jsonResponse.getBoolean("success");
                        if (success) {

                            int vat = jsonResponse.getInt("vat");
                            Intent intent = new Intent(LoginActivity.this, ChoiceSelectActivity.class);
                            intent.putExtra("username", username);
                            intent.putExtra("vat", vat);
                            LoginActivity.this.startActivity(intent);

                        }else{

                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage ("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            };

            LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }

这是PHP文件:

{<?php
$con = mysqli_connect("xxx.com", "xxx", "xxx", "xxx");

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

$statement = mysqli_prepare($con, "SELECT * FROM Name 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,   $username,  $password,  $vat);

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

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

echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:1)

即使我遇到同样的问题。尝试同步gradle,单击图标。然后,gradle将完成构建,稍后您可以立即运行您的应用程序