gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:20 25 4
gpt4 key购买 nike

我试过一千种东西。截至目前,我查询任何内容的唯一方法是获取整个列表并以这种方式查看它!这需要很多时间。我如何在 google app engine 中查询某些内容,例如仅拉取投票数大于 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();

最佳答案

在 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 进行查询吗? + 查询字符串

编辑:预览!

This is a Preview release of Google Cloud Endpoints. As a result, the API is subject to change and the service itself is currently not covered by any SLA or deprecation policy. These characteristics will be evaluated as the API and service moves towards General Availability, but developers should take this into consideration when using the Preview release of Google Cloud Endpoints.

游标功能很可能仍在开发中。但我也不确定您为什么要使用 Cursors,因为 Collections 更容易使用......您难道不想做下面的事情然后上面的糟糕代码吗? :)

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

关于android - 使用游标从 android 中的谷歌应用引擎查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com