使用Android从Google App Engine查询实体

问题描述 投票:0回答:1

我已经尝试了上千种方法。 截至目前,我查询任何内容的唯一方法就是获取整个列表并以这种方式查看它! 这需要很多时间。 我该如何在Google App Engine中查询某些内容,例如仅拉出具有100多个投票的实体。

我按照井字游戏示例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();
}

/**
 * This method gets the entity having primary key id. It uses HTTP GET method.
 *
 * @param id the primary key of the java bean.
 * @return The entity with primary key id.
 */
@ApiMethod(name = "getquotes")
public quotes getquotes(@Named("id") String id) {
    EntityManager mgr = getEntityManager();
    quotes quotes = null;
    try {
        quotes = mgr.find(quotes.class, id);
    } finally {
        mgr.close();
    }
    return quotes;
}

尝试使用用户光标,但现在确定它如何工作。 我试过了

  Cursor cursor = db.rawQuery("select * from Votes WHERE Votes >" + 250 , null);
  quotes = endpoint.listquotes().setCursor(cursor).execute();
java android database web-services google-app-engine
1个回答
1
投票

您是否尝试过将参数传递给endpoint.listquotes()? 具体来说,参数“ limit”用于限制结果数量,而参数“ cursor”用于更改选择标准?

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