【问题标题】:How to get a variable from a php file and use it in a Android activity?如何从 php 文件中获取变量并在 Android 活动中使用它?
【发布时间】:2015-11-23 18:17:07
【问题描述】:

我创建了一个应用程序,它使用 web 服务器上的 mysql 数据库。我正在使用 php 文件插入一些记录,如下所示:

<?php
    if($_SERVER["REQUEST_METHOD"]=="POST") {
        require 'connection.php';
        createUsername();
    }

    function createUsername() {

        global $connect;

        $username = $_POST["username"]; 

        $query = " INSERT INTO statistics(username) VALUES ('$username');";

        $result = $connect->query($query);
        $lastInsertId = $connect->insert_id;

        header('Content-Type: application/json');
        echo json_encode($lastInsertId);
    }
?>

我需要在我的 Android 活动中使用 $lastInsertId 变量。我尝试使用 `VOLLEY.这是我的代码:

    queue = Volley.newRequestQueue(getApplicationContext());

    JsonObjectRequest jsonObjectRequestLastInsertId = new JsonObjectRequest(Request.Method.POST, insertUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response.toString());
            try {
                JSONArray ids = response.getJSONArray("lastInsertId");
                for (int i = 0; i < ids.length(); i++) {
                    JSONObject student = ids.getJSONObject(i);

                    String lastInsertId = student.getString(this);
                    Toast.makeText(getApplicationContext(), lastInsertId, Toast.LENGTH_LONG).show();
                }

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.append(error.getMessage());

        }
    });
    queue.add(jsonObjectRequestLastInsertId);

我错了吗?谢谢!

【问题讨论】:

  • 日志中的System.out.println(response.toString()); 行有什么内容?
  • 在行日志println返回id。但是为什么Toast 不起作用?你认为我需要将JSONObject 转换为字符串吗?
  • 显示来自System.out.println(response.toString());的字符串
  • 这就是我得到的:I/System.out: 111。是 id 需要的确切 id。
  • 111 不是有效的 json 字符串,因此无需将 String 转换为 JSONObject 只需使用 Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();

标签: java android mysql json android-activity


【解决方案1】:

您只发回一个号码作为回复。我会发送一些更 JSON 的东西,像这样:

echo json_encode(array("lastInsertId" => $lastInsertId));

您假设您的响应中有一个名为 lastInsertId 的 json 数组:

JSONArray ids = response.getJSONArray("lastInsertId");

上面的代码不正确。 lastInsertId 是您的 Android 代码中的一个键,它打开一个数值。如果您仍有问题,请在此处发表评论并提供更多详细信息。

【讨论】:

  • 感谢您的回答。是的,lastInsertId 是我的 Android 代码中的一个键,它打开一个数值,但我无法使用它。我还是有问题。我简化了代码,看起来像this 因此,使用此代码,系统System.out.println 的id 完美,但是当我尝试将int 转换为字符串时,Toast 不起作用。知道为什么吗?
  • 我也尝试使用:String.valueOf(response);Toast 包含null 值。
  • @AlexM.,您可以使用键从 JSONObject 获取值,例如看这里:json.org/javadoc/org/json/…
  • 使用密钥从 JSONObject 获取值解决了我的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多