- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个存储在云中的 Android 应用程序,带有 Google App Engine。我使用云端点。我的问题是我无法将数据从服务器发送到我的客户端(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 或任何其他类型的集合。
那我该怎么办呢?由于添加数据是如此简单直接,我假设检索数据将以相同的方式执行,但显然我遗漏了一些重要的事情。
提前致谢!
最佳答案
您在后端使用的类与您将在客户端使用的类不同。 Endpoints 将为您生成一组库,可以在命令行上使用,也可以使用 Google Plugin for Eclipse 等工具。参见 Using Endpoints in an Android Client .
生成的类表示 Note
的集合您示例中的 s 将被命名为类似 NotesCollection
的名称.这个对象将有一个方法 getItems
,它为您提供了 List<Note>
您可以在您的 Android 应用程序中进行迭代。
关于java - 如何从服务器端(Google App Engine、Cloud Endpoints)将信息发送到我的客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958136/
我是一名优秀的程序员,十分优秀!