Web Services 在Android中使用光标从谷歌应用程序引擎查询

wztqucjr  于 2022-11-15  发布在  Android
关注(0)|答案(2)|浏览(151)

我已经尝试了上千种方法。到目前为止,我查询任何东西的唯一方法是获得整个列表,并通过这种方式查看它!这需要很多时间。我如何在谷歌应用程序引擎中查询一些东西,例如只拉有〉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();
l2osamch

l2osamch1#

在Google App Engine中,您需要设置一个servlet来为您查询数据库,然后以JSON格式返回结果,请参阅此处了解更多信息:第https://developers.google.com/appengine/docs/java/#Requests_and_Servlets故事
您最终将使用http://your-url/query?+查询字符串进行查询

**编辑:**预览!

这是Google Cloud Endpoints的预览版。因此,API可能会更改,并且服务本身当前不受任何SLA或弃用策略的约束。这些特征将在API和服务向正式发布的过程中进行评估,但开发人员在使用Google Cloud Endpoints的预览版时应考虑到这一点。
最有可能的是光标功能还在开发中。但是我也不确定你为什么要使用光标,因为集合是如此容易使用......难道你不喜欢做下面的代码,而不是上面糟糕的代码吗?:)

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

wixjitnu2#

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

@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();
}

相关问题