Jac*_*ień 4 google-app-engine android google-cloud-endpoints
我正在尝试使用示例GAE/Android应用程序.有实体.
在生成的PlaceEndpoint类中有一个方法:
@ApiMethod(name = "listGame")
public CollectionResponse<Place> listPlace(
@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit) {
EntityManager mgr = null;
Cursor cursor = null;
List<Game> execute = null;
try {
mgr = getEntityManager();
Query query = mgr.createQuery("select from Place as Place");
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<Game>) 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 (Game obj : execute)
;
} finally {
mgr.close();
}
return CollectionResponse.<Game> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
Run Code Online (Sandbox Code Playgroud)
据我了解cursor和limit所有可选的参数.
但是,我无法弄清楚如何Placeednpoint在客户端使用类传递它们:
Placeendpoint.Builder builder = new Placeendpoint.Builder(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
builder = CloudEndpointUtils.updateBuilder(builder);
Placeendpoint endpoint = builder.build();
try {
CollectionResponsePlace placesResponse = endpoint.listPlace().execute();
} catch (Exception e) {
e.printStackTrace();
Run Code Online (Sandbox Code Playgroud)
通常,当params不可为空时,我会在endpoint.listPlace()方法中传递它们.但是当params可以为空时,客户端应用程序看不到替代构造函数,那将接受params.
那我该怎么办呢?
要在通过云端点发送查询请求时从客户端传递参数,您需要添加用于设置参数的设置.要从android发送所需的参数,您将定义REST路径和方法类型的类应包括设置游标和限制的选项.例如,对于String cursorstring:
@com.google.api.client.util.Key
private String cursorstring;
public String getCursorstring() {
return cursorstring;
}
public ListPlace setCursorstring(String cursorstring) {
this.cursorstring = cursorstring;
return this;
}
Run Code Online (Sandbox Code Playgroud)
最后,当你从android代码调用端点方法时,你应该使用setCursorstring传递一个值,它将类似于:
CollectionResponsePlace placesResponse = endpoint.listPlace().setCursorstring("yourcursorstring").execute();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |