2012-09-18 68 views
0

我的目標是以指定的結果號請求GoogleTaskAPI for TASKLIST。 它工作正常,如果我傳遞沒有requestBody。但我需要將請求參數傳遞給要返回的特定結果數。當我這樣做時,它會創建新的任務列表,而不是列出。那麼如何做到這一點?帶有正文內容的Google API Java GET請求

我的代碼:

GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken, httpTransport, jsonFactory, clientId, clientSecret, refreshToken); 
    HttpRequestFactory rf = httpTransport.createRequestFactory(access); 

    String endPointUrl = "https://www.googleapis.com/tasks/v1/users/@me/lists"; 
    String requestBody = "{\"maxResults\":3}"; 

    GenericUrl endPoint = new GenericUrl(endPointUrl); 
    ByteArrayContent content = new ByteArrayContent("application/json", requestBody.getBytes()); 

    //Try 0: Works, But Retrieving all of my Tasklist, I need only 3 
    //HttpRequest request = rf.buildGetRequest(endPoint); 
    //------- 

    //Try 1: Fails to retrieve 
    //HttpRequest request = rf.buildGetRequest(endPoint); 
    //request.setContent(content); 
    //request.getContent().writeTo(System.out); 
    //------- 

    //Try 2: Fails to retrieve 
    HttpRequest request = rf.buildRequest(HttpMethod.GET, endPoint, content); 
    request.getContent().writeTo(System.out); 
    //------- 

    HttpResponse response = request.execute(); 
    String str = response.parseAsString(); 
    utils.log(str); 

回答

1

maxResults是查詢參數,而不是一個請求參數,所以你可以把它放在網址:

String endPointUrl = "https://www.googleapis.com/tasks/v1/users/@me/lists?maxResults=3"; 

你也應該考慮使用Java客戶端的任務接口提出請求;

http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API

+0

GR8,它的工作原理真棒:因爲它處理的URL的詳細信息,你可能更容易一些。但我想知道爲什麼以前它創建新的空_tasklist_而不是拋出錯誤響應或默認_tasklist_ –