Search code examples
androidgoogle-app-enginegoogle-cloud-endpoints

@Nullable @Named in GAE / Android Sample


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?


Solution

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