【问题标题】:Android: Get data from MySQL DatabaseAndroid:从 MySQL 数据库中获取数据
【发布时间】:2015-10-24 08:59:57
【问题描述】:

我已经观看了超过 5 个小时的 YouTube 教程,但我根本不知道如何完成这项工作。总有一些我不明白或对我不起作用的东西。!

我想从数据库中获取数据并将其带到我的 Android 中。我读过这可能适用于 JSON。

这是我的 JSON 数据库导出的链接。这是正确的吗?

http://web2page.ch/apps/FruityNumber/recordShow2.php

我使用的 API: compileSdkVersion 23

我希望你能一步一步地帮助我。我正在寻找最简单的方法。

【问题讨论】:

    标签: java android mysql json database


    【解决方案1】:

    有几种方法可以执行此操作。您可以向服务器执行 Http 请求并获取结果,手动将它们解析为 java 对象。或者使用像 Gson 这样的库,它可以节省您手动解析并将 JSON 字符串序列化为 java 对象的工作。

    但我建议使用 Retrofit 一个 Http 客户端,它可以让您轻松下载 JSON 数据并将其解析为 POJO(普通旧 Java 对象)。为此,Retrofit 使用了 Gson、Jackson、Moshi 等转换器。

    使用 Retrofit 相当简单,

    1. 定义接口(同步或异步)并定义 API 端点
    2. 使用基本 URL 构建 Retrofit 对象
    3. 如果执行同步请求,改造将为您的接口生成一个实现,您必须确保在后台线程中发出请求
    4. 如果执行异步请求,您将需要注册一个回调,您将通过该回调获得响应

    最后一个 POJO 类供您响应,

    public class Item {
        public String id;
        public String user;
        public String highscore;
    }
    

    Here 是关于如何使用 Retrofit 的完整指南。

    【讨论】:

      【解决方案2】:

      使用像 GSON 这样的库将 JSON 转换为 Java 对象会更容易。

      它的工作方式你创建一个模型类例如:

      import javax.annotation.Generated;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;
      
      @Generated("org.jsonschema2pojo")
      public class Result{
      
      @SerializedName("id")
      @Expose
      private String id;
      @SerializedName("user")
      @Expose
      private String user;
      @SerializedName("highscore")
      @Expose
      private String highscore;
      
      /**
      * 
      * @return
      * The id
      */
      public String getId() {
      return id;
      }
      
      /**
      * 
      * @param id
      * The id
      */
      public void setId(String id) {
      this.id = id;
      }
      
      /**
      * 
      * @return
      * The user
      */
      public String getUser() {
      return user;
      }
      
      /**
      * 
      * @param user
      * The user
      */
      public void setUser(String user) {
      this.user = user;
      }
      
      /**
      * 
      * @return
      * The highscore
      */
      public String getHighscore() {
      return highscore;
      }
      
      /**
      * 
      * @param highscore
      * The highscore
      */
      public void setHighscore(String highscore) {
      this.highscore = highscore;
      }
      
      }
      

      这很容易:

      Gson gson = new Gson();
      List<Result> results = gson.fromJson(json, new TypeToken<List<Result>>(){}.getType());
      

      例子:

      for(Result res : results){
        Log.d(LOG_TAG, "id = " + res.getId());
      }
      

      【讨论】:

        【解决方案3】:

        有很多方法可以完成您的工作。但我会选择简单的。

        1. 首先通过发出 HTTP 请求将所有 json 数据从服务器端带到客户端(指应用端)。您可以使用 android 首选 HttpUrlConnection 类。您可以访问此处进行操作。

        http://syntx.io/how-to-send-an-http-request-from-android-using-httpurlconnection/

        1. 将数据带到应用程序端后。您必须使用 JSONObject 和 JSONArray 类解析该 json 格式数据。为此,您必须创建一个模型类。为此,您可以访问

        http://androidexample.com/JSON_Parsing_-_Android_Example/index.php?view=article_discription&aid=71&aaid=95

        我认为你应该像这样改变你的 json 格式:

        {
            "Results": [
                {
                    "id": "1",
                    "user": "ero",
                    "highscore": "13 Sek"
                },
                {
                    "id": "2",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "3",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "4",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "5",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "6",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "7",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "8",
                    "user": "rick",
                    "highscore": "21 Sek"
                },
                {
                    "id": "9",
                    "user": "",
                    "highscore": ""
                },
                {
                    "id": "10",
                    "user": "Stefano",
                    "highscore": "alle"
                },
                {
                    "id": "11",
                    "user": "Test001",
                    "highscore": "Test001"
                },
                {
                    "id": "12",
                    "user": "",
                    "highscore": ""
                },
                {
                    "id": "13",
                    "user": "Stefano",
                    "highscore": "5sek"
                },
                {
                    "id": "14",
                    "user": "sven",
                    "highscore": "1"
                },
                {
                    "id": "15",
                    "user": "sven",
                    "highscore": "1"
                },
                {
                    "id": "16",
                    "user": "sven",
                    "highscore": "1"
                },
                {
                    "id": "17",
                    "user": "",
                    "highscore": ""
                },
                {
                    "id": "18",
                    "user": "kiala",
                    "highscore": ""
                },
                {
                    "id": "19",
                    "user": "kiala",
                    "highscore": "angola"
                },
                {
                    "id": "20",
                    "user": "kiala",
                    "highscore": ""
                }
            ]
        }
        

        它看起来非常相关且易于解析。要更改 json 格式,您可以在 php 文件中编写代码。

        $sql = mysqli_query("SELECT ...");
        $resluts = array();
        while($row = mysqli_fetch_assoc($sql)) {
            $tmp = array();
            $tmp["id"] = $row["id"];
            $tmp["user"] = $row["user"];
            $tmp["highscore"] = $row["highscore"];
            array_push($results["Results"], $tmp);
        }
        echo json_encode($results);
        

        【讨论】:

        • 谢谢,我要如何更改格式?我只是使用了 print(json_encode($user_array))。
        • 我使用了你的 php 代码,但它不起作用 ---> web2page.ch/apps/FruityNumber/recordShow2.php &lt;?PHP include "inc_mysql.php"; $auslesen = "SELECT * FROM FruityNumber"; $sql = mysql_query($auslesen); $results = array(); while($row = mysql_fetch_assoc($sql)) { $tmp = array(); $tmp["id"] = $row["id"]; $tmp["user"] = $row["user"]; $tmp["highscore"] = $row["highscore"]; array_push($results["Results"], $tmp); } echo json_encode($results); ?&gt; 我已将“mysqli_fetch”更改为“mysql_fetch...”
        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 2017-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多