【问题标题】:@Nullable @Named in GAE / Android SampleGAE / Android 示例中的@Nullable @Named
【发布时间】:2013-12-07 11:58:45
【问题描述】:

我正在尝试开发示例 GAE / Android 应用程序。有地方实体。

在生成的 PlaceEndpoint 类中有一个方法:

@ApiMethod(name = "listGame")
    public CollectionResponse<Place> listPlace(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

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

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

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

据我了解 cursorlimit 所有可选参数。

但是我不知道如何在客户端使用 Placeednpoint 类传递它们:

Placeendpoint.Builder builder = new Placeendpoint.Builder(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
builder = CloudEndpointUtils.updateBuilder(builder);
Placeendpoint endpoint = builder.build();

try {
    CollectionResponsePlace placesResponse = endpoint.listPlace().execute();
} catch (Exception e) {
    e.printStackTrace();

通常,当参数不可为空时,我会将它们传递给 endpoint.listPlace() 方法。但是当参数可以为空时,客户端应用程序看不到可以接受参数的替代构造函数。

那我应该如何通过它们呢?

【问题讨论】:

    标签: android google-app-engine google-cloud-endpoints


    【解决方案1】:

    为了在通过云端点发送查询请求时从客户端传递参数,您需要添加设置参数的规定。要从 android 发送所需的参数,您将在其中定义 REST 路径和方法类型的类应包含设置光标和限制的选项。例如对于字符串 cursorstring

    @com.google.api.client.util.Key
          private String cursorstring;
    
          public String getCursorstring() {
            return cursorstring;
          }
    
          public ListPlace setCursorstring(String cursorstring) {
            this.cursorstring = cursorstring;
            return this;
          }
    

    最后,当从你的 android 代码调用端点方法时,你应该使用 setCursorstring 传递一个值,类似于:

    CollectionResponsePlace placesResponse = endpoint.listPlace().setCursorstring("yourcursorstring").execute();
    

    【讨论】:

    • 那么@Nullable 参数是干什么用的?我也可以删除它对吗?
    • 我不确定基于 java 的后端,因为我使用的是 python。请确认使用 java 的谷歌云端点的示例代码。对于您需要通过云端点从 android 发送的任何参数,请为每个参数使用上面提到的 setter 方法,并在您的应用引擎后端处理它们。如果您没有在后端代码中使用任何变量,您可以尝试删除
    • 您将如何使用 iOS 客户端库来做到这一点?有人知道吗?
    【解决方案2】:

    还有一种方法。您可以像这样简单地将您的可为空值“光标”设置为:

    CollectionResponsePlace placesResponse = 
         endpoint.listPlace().set("cursor", "Your Cursorstring").execute();
    

    在哪里可以

    【讨论】:

      【解决方案3】:

      面临同样的问题,但后端使用 Java 开发。 Tony m 提供的解决方案非常有效。 只需调整该方法在 Android 上的调用方式即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        • 2011-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多