【问题标题】:Data from Android App -> SQL server来自 Android 应用程序的数据 -> SQL 服务器
【发布时间】:2011-05-29 00:13:17
【问题描述】:

我正在开发一个 android 应用程序,用于收集数据,然后将其发送到 web 目录。

假设您想在手机上收集一组数据,然后在单击按钮后将其全部以文件或流的形式发送到在线目录。它甚至不需要得到响应——尽管将来确认会很方便。

这是对事物顺序的猜测......

dir = "someurl.com/data/files_received";
Array data;
sendDataSomehow(dir, data); //obv the difficult bit!

尽管我有丰富的 Web 编码经验,但我仍处于 Android 开发的早期阶段,所以这一点会很好。

我找到了一些关于 JSON、Google GSON、HTTP POST 和 GET 的建议 - 这些听起来是不是正确的轨道?

我希望我已经足够清楚了。

【问题讨论】:

    标签: android arrays


    【解决方案1】:

    是的,JSON 将是一个很好的解决方案。

    将您的数组编码为 JSON,然后将其作为 HTTP POST 请求的正文发送到您的 Web 服务器。如果你有一个小时可以消磨时间,这是 Google IO 去年发布的一个非常好的视频,解释了如何在 Android 上实现 REST 客户端(你所做的并不是严格意义上的 REST-ful,但是你对服务器的调用是非常相似):http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

    【讨论】:

      【解决方案2】:

      好的,只是想快速感谢您让我走上正轨。刚刚度过了其中一个 THE CODE WORKS EUREKA 时刻,非常高兴。我没有使用 JSON,但我设法通过 HTTP-POST 和一点点 PHP 将变量从 Android 传递到 SQL。

      我确信这不是推荐的意识形态,原因有很多,尽管对于原型和演示来说它会很好!

      这是安卓的代码:

      try {
              URL url = new URL("http://www.yourwebsite.com/php_script.php");
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("POST");
              conn.setDoOutput(true);
              conn.setDoInput(true);
              conn.setUseCaches(false);
              conn.setAllowUserInteraction(false);
              conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      
              OutputStream out = conn.getOutputStream();
              Writer writer = new OutputStreamWriter(out, "UTF-8");
              writer.write("stringToPass=I'd like to pass this");
              writer.close();
              out.close();
      
              if(conn.getResponseCode() != 200)
              {
                  throw new IOException(conn.getResponseMessage());
              }
      
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              StringBuilder sb = new StringBuilder();
              String line;
              while ((line = rd.readLine()) != null)
              {
                  sb.append(line);
              }
              rd.close();
      
              conn.disconnect();
          } catch (MalformedURLException e1) {
              textBox.setText(e1.toString());
          } catch (IOException e2) {
              textBox.setText(e2.toString());
          }
      

      这里是 PHP 的代码:

      $conn = mysql_connect("localhost","web108-table","********") or die (mysql_error());
      mysql_select_db("web108-table",$conn) or die (mysql_error());
      
      $str = $_POST['stringToPass'];
      
      mysql_query("INSERT INTO table(field) VALUES ($str)");
      

      此代码有效,非常简单。接下来的测试将是找出它是否适合大量字符串。

      我希望这对其他人有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2020-09-09
        相关资源
        最近更新 更多