【问题标题】:Query entities from google app engine using android使用android从谷歌应用引擎查询实体
【发布时间】:2013-07-18 23:58:45
【问题描述】:

我已经尝试了一千种方法。截至目前,我查询任何内容的唯一方法是获取整个列表并以这种方式查看!这需要很多时间。如何在谷歌应用引擎中查询某些内容,例如仅拉取超过 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】:

    您是否尝试将参数传递给 endpoint.listquotes()? 具体参数“limit”来限制结果的数量和参数“cursor”来改变选择标准?

    【讨论】:

    • 感谢您的回复!你的意思是这样的吗?你能告诉我一个光标如何在这里工作的例子吗?我在下面试过这个
    • Cursor cursor = db.rawQuery("select * from Votes WHERE Votes >" + 250 , null);引号 = endpoint.listquotes().setCursor(cursor).execute();
    • 我没有定义数据库,因为我不需要
    • 为你更新了我的问题的结尾
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多