2013-11-22 37 views
4

我正在嘗試使用示例GAE/Android應用程序。有地方實體。@Nullable @在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所有可選PARAMS。

但我無法弄清楚如何使用在客戶端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(); 

通常情況下,當PARAMS不可爲空我會通過他們endpoint.listPlace()方法。但是當params可以爲空時,客戶端應用程序不會看到替代構造函數,它會接受params。

我該怎麼通過它們呢?

回答

9

要通過雲終端發送查詢請求時從客戶端傳遞參數,需要添加設置參數的設置。要從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(); 
+0

那麼@Nullable參數是什麼?我可以刪除它嗎? –

+0

我不確定基於java的後端,因爲我使用python。請使用java確認谷歌雲終端的示例代碼。無論您需要通過雲端點從Android發送什麼參數,都需要爲每個參數提供上述的setter方法,並在您的應用引擎後端處理它們。如果您在後端代碼中未使用任何變量,則可以嘗試刪除 –

+0

就是這樣!謝了哥們! –

1

有是另一種方式。你可以簡單地設置您的空值「光標」這樣的:

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

在這裏你可以

0

面臨着同樣的問題,但使用Java後端開發。 Tony m提供的解決方案非常有魅力。 只需調整在Android上調用該方法的方式,即可正常工作。