如何从服务器端(Google App Engine,Cloud Endpoints)向我的客户端发送信息?

问题描述 投票:4回答:2

我有一个使用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或任何其他类型的集合。

那我该怎么办? 由于添加数据非常容易且如此直接,我假设检索数据将以相同的方式执行,但显然我遗漏了对此事必不可少的内容。

先感谢您!

java android google-app-engine cloud google-cloud-endpoints
2个回答
4
投票

您在后端使用的类与您在客户端中使用的类不同。 端点将为您生成一组库,可以在命令行上使用,也可以使用Google Plugin for Eclipse等工具。 请参阅在Android客户端中使用端点

表示示例中Note的集合的生成类将命名为NotesCollection 。 该对象将有一个方法getItems ,它为您提供了一个List<Note>您可以在Android应用程序中进行迭代。


0
投票

与将数据插入数据存储模型(POST类型的方法)的端点类似,您需要有一个端点来查询数据存储模型中的数据(GET类型的方法)。 定义这两种方法后,需要生成发现文档和客户端库,以便客户端了解这两种方法并调用它们。 如果您谈到在Web上显示数据,那么您可以使用所需的客户端库来构建Javascript客户端。

© www.soinside.com 2019 - 2024. All rights reserved.