java - 基本的 Android 应用方向

标签 java android mysql sql

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

编辑:只是为了好玩。

最佳答案

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

这里是您可以绑定(bind)到一个按钮的代码示例,该按钮将发送存储在字段 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();
    }
  }

关于java - 基本的 Android 应用方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944466/

相关文章:

java - android音乐播放器获取上次播放列表

java - SQL 替换与执行插入或更新语句

java - 将字段添加到表并重新生成后保留对 JPA 实体所做的自定义更改

android - 为什么我需要在 Android 中扩展 Activity?

mysql - 在 Rails 中动态保存分数排名

mysql - 在社交网络中使用图数据库有哪些优势?

php - 插入一个包含多个值的数组,其中一些值本身就是一个数组到数据库

java - 更改数组值后打印数组

android - air for android 图标尺寸太小

Android Knockout 选择问题 - Chrome 和 native 浏览器