1

Eclipse と Java Android SDK を使用する前に取ったさまざまなプログラミング クラスで、基本的な Android アプリを作成しました。私が作成したいアプリでは、後で分析される情報をユーザーが入力する必要があります。このデータを他の人のデータと比較できるようにしたいので、ユーザーが作成したすべてのエントリをデータベースに送信して、後でデータを比較しようとしたときにクエリを実行できるようにしたいと考えています。これを達成する方法についての指示が欲しいです。無料のホスティング サイトを見つけて Sql サーバーをセットアップする必要がありますか、それともこれを実現するためのより良い方法はありますか?

編集:ただの楽しみです。

4

3 に答える 3

2

私は非常に初心者の 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();
    }
  }
于 2013-06-05T16:13:59.387 に答える
0

シンプルで、DB は必要ありません。

Usergrid by Apigee はまさにあなたが探しているものです!

  1. 各ユーザーの詳細を保存できます
  2. 保存されたデータを取得する
  3. デバイス間でイベントを送信し、イベント コールバックを受信する

何よりも、サーバー側のコードはありません。APIのみ

参考までに、これは、サーバーのコーディング方法を知っている場合でも、向かうべき方向です。

PS: 私は apigee や usergrid では働いていません。

于 2013-06-05T16:41:40.730 に答える