【问题标题】:JSON to Android getStringJSON到Android getString
【发布时间】:2017-09-19 02:01:22
【问题描述】:

我目前正在获取 Officer Name 但我想添加 Reference 但我不知道我是否正确执行了该过程以及如何在 android 上获取参考值?目前,我正在应用程序上的活动开始时敬酒官员的名字。我想添加对 toast 的引用,但我不知道如何获取它的值。

php 脚本

<?php

require "init.php";
$user_name = $_POST["user_name"];
$password = $_POST["password"];
$activate;
$sql = "select officer_name,reference from user_tbl where user_name like '" . $user_name . "' and password like '" . $password . "' and activate = 1;";

$result = mysqli_query($con, $sql);
$response = array();
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_row($result);
    $officer_name = $row[0];
    $reference = $row[1];
    $code = "Login_Success";
    array_push($response, array("code" => $code,
        "officer_name" => $officer_name,
        "reference" => $reference));
    echo json_encode($response);
} else {
    $code = "Login_Failed";
    $message = "Error! User not found or Not Activated";
    array_push($response, array("code" => $code, "message" => $message));
    echo json_encode($response);

}

mysqli_close($con);

?>

安卓代码

 StringRequest stringRequest = new StringRequest(Request.Method.POST, loginUrl,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONArray jsonArray = new JSONArray(response);
                                    JSONObject jsonObject = jsonArray.getJSONObject(0);

                                    String code = jsonObject.getString("code");
                                    if (code.equals("Login_Failed"))
                                    {
                                        builder.setTitle("Login Error");
                                        displayAlert(jsonObject.getString("message"));
                                    }
                                    else
                                    {
                                        Intent intent = new Intent(Login.this,MainActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putString("officer_name", jsonObject.getString("officer_name"));
                                        intent.putExtras(bundle);
                                        startActivity(intent);

【问题讨论】:

  • jsonObject.getString("reference") 呢?
  • 完美!我太傻了。谢谢

标签: php android json mysqli


【解决方案1】:

使用 jsonObject.getString 方法,如jsonObject.getString("reference")

this for reference

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多