【问题标题】:connect to websites sql database and show web content on android连接到网站 sql 数据库并在 android 上显示 web 内容
【发布时间】:2012-02-15 16:02:46
【问题描述】:

我爸爸想让我为他的网站制作一个 android 和 iPhone 应用程序,我还在学习 java 并且正在先做一个 android 应用程序。我正在寻找的是如何连接到他的 sql 数据库,这样我就可以让他们从应用程序中登录,然后显示网站内容,但不是在 webview 中这将如何工作?你能指点我一些教程吗?

【问题讨论】:

    标签: android sql database


    【解决方案1】:

    您应该创建一个web service 并使用一个JSON API 或其他一些数据交换格式。

    【讨论】:

      【解决方案2】:

      您不应该使数据库可以从 Internet 访问!如果您对服务器配置不是很熟悉,这是非常危险的。通过 http 传输 instat 数据。例如。 JSON 容器中的 PHP 脚本。

      查找mysql_fetch_assoc 函数和json-encode 函数。

      另请参阅这个简单且不安全的示例:

      <?php
      $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
      
      if (!$conn) {
          echo "Unable to connect to DB: " . mysql_error();
          exit;
      }
      
      if (!mysql_select_db("mydbname")) {
          echo "Unable to select mydbname: " . mysql_error();
          exit;
      }
      
      $result = mysql_query("SELECT * FROM sometable");
      
      if (!$result) {
          echo "Could not successfully run query ($sql) from DB: " . mysql_error();
          exit;
      }
      
      if (mysql_num_rows($result) == 0) {
          echo "No rows found, nothing to print so am exiting";
          exit;
      }
      
      $rows=array();
      // While a row of data exists, put that row in $row as an associative array
      // Note: If you're expecting just one row, no need to use a loop
      // Note: If you put extract($row); inside the following loop, you'll
      //       then create $userid, $fullname, and $userstatus
      while ($row = mysql_fetch_assoc($result)) {
          $rows[]=$row;
      }
      
      // output in JSON format
      echo(json_encode($rows));
      
      mysql_free_result($result);
      
      ?>
      

      【讨论】:

      • 好吧,php 脚本是有道理的,我知道一点 php,但我如何在 android 应用程序中使用它?
      • 您必须下载文件并解码文件。请参阅this question 了解如何执行此操作。另请记住,您需要互联网许可。
      猜你喜欢
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多