【发布时间】:2020-04-15 22:43:36
【问题描述】:
我正在阅读一个 android 方法中的 php 响应。
PHP
if(mysqli_num_rows($result)>0) {
$status = "Success!";
echo "Success!";
} else {
echo "Login failed";
}
安卓
@Override
protected void onPostExecute(String result) {
if(result.equals("Success!")) {
Toast.makeText(context, "Login Successful!", Toast.LENGTH_LONG).show();
Intent goToWelcomePage = new Intent(context, welcome.class);
context.startActivity(goToWelcomePage);
} else if (result.equals("User Created")){
Toast.makeText(context, "User Created. Login Now.", Toast.LENGTH_LONG).show();
Intent goToLoginPage = new Intent(context, MainActivity.class);
context.startActivity(goToLoginPage);
} else {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
上面的脚本运行没有任何问题。
但是,我想读取结果数组。我在 PHP 中尝试过。
$status = array();
if(mysqli_num_rows($result)>0) {
$status = "Success!";
echo json_encode(array("result"=>$status,"name"=>$username));
} else {
$status = "Login failed";
echo json_encode(array("result"=>$status));
}
不确定如何在 Android 中阅读此内容。此外,我不确定上面的php是否是将结果返回给android的正确方法。
【问题讨论】:
-
php 部分无关紧要。您可能需要编辑您的问题以包含您的服务器为此调用返回的 json 响应