4

Google App Engine を使用して、クラウドに格納されている Android アプリケーションがあります。Cloud Endpoints を使用しています。私の問題は、サーバーからクライアント (Android デバイス) にデータを送信できないことです。つまり、これまでのところ、その方法がわかりません。

これまでのところ、次のコードを使用して、エンドポイントを作成し、データベースへのレコードの追加を担当するメソッド (サーバー側、myProject - AppEngine にあります) を呼び出すことで、データストアにデータを挿入することができました。 (クライアント上):\

 Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
 AndroidHttp.newCompatibleTransport(),
 new JacksonFactory(),
 new HttpRequestInitializer() {
 public void initialize(HttpRequest httpRequest) { }
 });
  Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();
  try {
      // Construct the note.
      Note note = new Note().setDescription("Note DescriptionRoxana");
      String noteID = new Date().toString();
      note.setId(noteID);

      note.setEmailAddress("E-Mail AddressRoxana");        
      // Insert the Note, by calling a method that's on the server side - insertNote();
      Note result = endpoint.insertNote(note).execute();
  } catch (IOException e) {
    e.printStackTrace();
  }

しかし、データストアからデータを取得してサーバー側に表示する方法がわかりません。データベース内のすべてのレコードを取得するメソッド (サーバー上にあるメソッド) を呼び出すエンドポイントを作成して、同じことを試みましたが、アプリケーションがクラッシュしました。

データストアからデータを取得するメソッドのコードは次のとおりです。

    public CollectionResponse<Note> listNote(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Note> execute = null;

    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery("select from Note as Note");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<Note>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (Note obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<Note> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}

ご覧のとおり、返される型はコレクション レスポンスです。次のインポートを実行すると、このタイプのデータにアクセスできます。

   import com.google.api.server.spi.response.CollectionResponse;

これはサーバー側のデータ型の特徴であると推測したため、クライアント側で使用できる List、ArrayList、またはその他の種類のコレクションにキャストする方法がわかりません。

ではどうすればいいのでしょうか?データの追加は非常に簡単で簡単だったので、データの取得も同じ方法で実行されると想定していましたが、どうやらこの問題に不可欠なものが欠けているようです。

前もって感謝します!

4

2 に答える 2

0

データストア モデルにデータを挿入するためのエンドポイント (タイプ POST のメソッド) を持つのと同様に、データストア モデルからデータを照会するためのエンドポイント (タイプ GET のメソッド) が必要です。これらの両方のメソッドを定義したら、ディスカバリ ドキュメントとクライアント ライブラリを生成して、クライアントがこれらのメソッドを認識し、呼び出すことができるようにする必要があります。Web 自体にデータを表示することについて話している場合は、必要なクライアント ライブラリを使用してJavascript クライアントを構築できます。

于 2013-05-21T12:37:01.633 に答える