【问题标题】:Basic Android App Direction基本 Android 应用方向
【发布时间】:2013-06-01 10:06:24
【问题描述】:

在使用 Eclipse 和 Java Android SDK 之前,我已经在各种编程课程中创建了基本的 Android 应用程序。 我想创建的应用程序需要用户输入稍后将被分析的信息。我希望人们能够将这些数据与其他人的数据进行比较,因此我希望用户所做的每个条目都提交到数据库,以便稍后在有人尝试比较他们的数据时进行查询。 我想知道如何做到这一点的方向。我应该找到一个免费的托管站点并设置一个 Sql 服务器还是有更好的方法来完成这个?

编辑:只是为了好玩。

【问题讨论】:

  • 这是商业服务还是开源还是只是为了好玩?
  • 第一个方向:最流行的开源数据库叫mysql,不是mysqli
  • (编辑后)对于实施阶段,我会在您的开发机器上本地安装一些数据库。后来 - 如果它真的只是为了好玩,我会寻找 LucasSeveryn 提到的便宜甚至免费的在线数据库。
  • 别忘了选择你最喜欢的答案!

标签: java android mysql sql


【解决方案1】:

我是一个非常初级的android开发者,我发现使用像mongolab.com这样的云存储在线数据库对用户提交的数据非常友好。数据库和服务器之间的通信必须通过 JSON 解析和 URI 请求来完成。

这是您可以绑定到按钮的代码示例,该按钮将发送存储在字段 tempData 中的对象:

public void send(View view) {
    String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA?apiKey="
        + apiKey;
    try {

      // make web service connection
      final HttpPost request = new HttpPost(apiURI);
      request.setHeader("Accept", "application/json");
      request.setHeader("Content-type", "application/json");

      // Build JSON string with GSON library
      Gson gson = new Gson();

      JsonElement jsonElement = gson.toJsonTree(tempData);
      String json = gson.toJson(jsonElement);
      StringEntity entity = new StringEntity(json);

      Log.d("****Parameter Input****", "Testing:" + json);
      request.setEntity(entity);
      // Send request to WCF service
      final DefaultHttpClient httpClient = new DefaultHttpClient();

      new AsyncTask<Void, Void, Void>() {
        @Override
        public Void doInBackground(Void... arg) {
          try {
            HttpResponse response = httpClient.execute(request);
            Log.d("WebInvoke", "Saving: "
                + response.getStatusLine().toString());
            // Get the status of web service
            BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity()
                    .getContent()));
            // print status in log
            String line = "";
            while ((line = rd.readLine()) != null) {
              Log.d("****Status Line***", "Webservice: " + line);

            }
          } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
          }
          return null;
        }
      }.execute();

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

  }

下面是用于检索数据库中元素的代码示例:

public void load() {
    String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA"
        + "?apiKey=" + apiKey;

    Log.d("****Status Line***", "" + apiURI);

    try {

      // make web service connection
      final StringBuilder builder = new StringBuilder();

      final HttpGet request = new HttpGet(apiURI);
      request.setHeader("Accept", "application/json");
      request.setHeader("Content-type", "application/json");
      final DefaultHttpClient httpClient = new DefaultHttpClient();

      new AsyncTask<Void, Void, String>() {

        @Override
        protected void onPostExecute(String result) {
          super.onPostExecute(result);
          doSomethingWithReceivedData(result); //THIS METHOD IS DEFINED IN BODY OF YOUR ACTIVITY
        }

        @Override
        public String doInBackground(Void... arg) {
          try {
            HttpResponse response = httpClient.execute(request);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {

              HttpEntity entity = response.getEntity();
              InputStream content = entity.getContent();

              BufferedReader reader = new BufferedReader(
                  new InputStreamReader(content));
              String line;
              while ((line = reader.readLine()) != null) {
                builder.append(line);
              }
              Log.d("****Status Line***", "Success");

              return builder.toString();

            } else {
              Log.d("****Status Line***",
                  "Failed to download file");
            }

          } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
          }
          return null;
        }
      }.execute();

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

【讨论】:

  • +1 不错的答案。 @Reed B:如果Redis 更符合您的口味:RedisCloud
  • 我试图回答,因为我希望看到我的问题得到解答,我发现逆向工程示例比阅读文档要容易得多。
  • 感谢您的帮助!由于这是一个休闲项目,我会尝试使用像 000webhost 这样的免费虚拟主机。
【解决方案2】:

您应该有一个数据库来存储数据。如上所述,数据库最好放在 MySQL (SQL) 中。您的应用程序应该有一个可以将结果 POST 到服务器的方法,服务器将读取字符串发送并检索和存储数据。 一个好的开始是阅读JSON 并阅读Asynctask

您还需要知道如何构建您的服务器部分。一个好主意是从 PHP 开始,但我不是该领域的专家。 我希望这可以帮助您开始您的项目。

【讨论】:

  • 我并不是说 php 是强制性的。对服务器进行编程与对 android 应用程序进行编程是不同的。服务端可以有多种实现方式,说不定他也可以用JAVA或者JSP实现……
  • 好吧,我什至不认为 PHP 是一个好的起点。更好的方法是找到与 CRUD API 一起使用的免费数据库,因此您无需编写服务器。对于初学者来说,它应该尽可能简单。他不应该与双方的部队打交道。
【解决方案3】:

简单,无需数据库。

Usergrid by Apigee 正是您想要的!

  1. 您可以存储每个用户的详细信息
  2. 检索存储的数据
  3. 跨设备发送事件和接收事件回调

最好的 - 没有服务器端代码。仅限 API

仅供参考,即使您知道如何编写服务器代码,这也是您应该前进的方向。

PS:我不为 apigee 或 usergrid 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多