@Nullable @Named in GAE / Android Sample

2020-02-29 06:35发布

I'm trying to work on the sample GAE / Android app. There is Place Entity.

In generated PlaceEndpoint class there is a method:

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

As I understand cursor and limit all optional params.

However I can't figure out how to pass them using Placeednpoint class on the client side:

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

Normally, when params are not nullable I would pass them in endpoint.listPlace() method. But when params are nullable, client side app doesn't see alternative constructor, that would accept params.

How am I supposed to pass them then?

3条回答
干净又极端
2楼-- · 2020-02-29 07:18

There is an additional way. You can simply set your nullable value 'cursor' it like this:

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

Where you can

查看更多
放我归山
3楼-- · 2020-02-29 07:19

Faced the same issue but backend developed using Java. The solution provided by Tony m worked like a charm. Simply had to adapt how the method is called on Android and it worked.

查看更多
ら.Afraid
4楼-- · 2020-02-29 07:23

For passing parameters from client side while sending a query request through cloud endpoints, you need to add provision for setting parameters. To send the required parameter from android , the class where you would define the REST Path and method type, should include an option to the set cursor and limit. For example for the String 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;
      }

Finally while calling the endpoint method from your android code, you should pass a value using the setCursorstring, which will be something like:

CollectionResponsePlace placesResponse = endpoint.listPlace().setCursorstring("yourcursorstring").execute();
查看更多
登录 后发表回答