使用光标从android中的谷歌应用引擎查询

时间:2013-07-20 04:45:01

标签: android database web-services google-app-engine cursor

我已经尝试了一千件事。到目前为止,我查询任何内容的唯一方法是获取整个列表并以这种方式查看它!这需要花费很多时间。如何在Google应用引擎中查询内容,例如只提取具有>的实体例如100票。

尝试用户光标但不确定它是如何工作的。我知道它可以使用光标,但我如何使用谷歌应用程序引擎进行设置,因为我的数据库不在我的应用程序中说?

我已经尝试过了......但这根本不起作用..

Cursor cursor = ("select * from Votes WHERE Votes >" + 250 , null);
quotes endpoint.listquotes().setCursor(cursor).execute();

  String query  = ("select * from Votes WHERE Votes >= 40");
    quotes endpoint.listquotes().setCursor(query).execute();

我正在关注井字游戏示例https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-javahttps://developers.google.com/eclipse/docs/endpoints-addentities在示例中,我只是更改了引号的注释。

以下是我当前的代码,例如关于如何获取实体。

 protected CollectionResponseQuotes doInBackground(Context... contexts) {

  Quotesendpoint.Builder endpointBuilder = new Quotesendpoint.Builder(
      AndroidHttp.newCompatibleTransport(),
      new JacksonFactory(),
      new HttpRequestInitializer() {
      public void initialize(HttpRequest httpRequest) { }
      });
  Quotesendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();
  try {


  quotes = endpoint.listquotes().execute();

   for (Quotes quote : quotes.getItems()) {

       if (quote.getVotes() > 3) {

       quoteList.add(quote);

        }  

 }

以下是Google在创建端点时在应用引擎中为我生成的代码。看起来它会以某种方式查询,但我无法弄明白。他们是两个不同的项目。

@Api(name = "quotesendpoint", namespace = @ApiNamespace(ownerDomain = "projectquotes.com"           ownerName = "projectquotes.com", packagePath = ""))
 public class quotesEndpoint {

/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listquotes")
public CollectionResponse<quotes> listquotes(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

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

try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from quotes as quotes");
    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<quotes>) 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 (quotes obj : execute)
        ;
} finally {
    mgr.close();
}

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

2 个答案:

答案 0 :(得分:3)

在Google App Engine中,您需要设置一个servlet来为您查询数据库,然后以JSON格式返回结果,有关详细信息,请参阅此处: https://developers.google.com/appengine/docs/java/datastore/queries https://github.com/octo-online/robospice https://developers.google.com/appengine/docs/java/#Requests_and_Servlets https://code.google.com/p/google-gson/

您最终会使用http:// your-url / query进行查询? +查询字符串

修改 预览!

  

这是Google Cloud Endpoints的预览版。结果,   API可能会发生变化,而服务本身目前不是   由任何SLA或弃用政策涵盖。这些特征会   被评估为API和服务走向通用   可用性,但开发人员应该考虑到这一点   使用Google Cloud Endpoints的预览版。

光标功能很可能仍处于开发阶段。但是我也不确定为什么你会想要使用游标,因为集合更容易使用...你不喜欢做下面那些可怕的代码吗? :)

ScoreCollection scores = service.scores().list().execute();

答案 1 :(得分:1)

更新您的列表方法以接受过滤器属性

@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listZeppaUserInfo")
public CollectionResponse<ZeppaUserInfo> listZeppaUserInfo(
        @Nullable @Named("filter") String filterString,
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    PersistenceManager mgr = null;
    Cursor cursor = null;
    List<ZeppaUserInfo> execute = null;

    try {
        mgr = getPersistenceManager();
        Query query = mgr.newQuery(ZeppaUserInfo.class);
        if (isWebSafe(cursorString)) {
            cursor = Cursor.fromWebSafeString(cursorString);
            HashMap<String, Object> extensionMap = new HashMap<String, Object>();
            extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
            query.setExtensions(extensionMap);
        } else if (isWebSafe(filterString)){
            // query has a filter
            query.setFilter(filterString);
        }

        if (limit != null) {
            query.setRange(0, limit);
        }

        execute = (List<ZeppaUserInfo>) query.execute();
        cursor = JDOCursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

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

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