繁体   English   中英

带有正文内容的Google API Java GET请求

[英]Google API Java GET request with body content

我的目标是请求带有指定结果编号的TASKLIST的GoogleTaskAPI。 如果我没有传递requestBody ,它工作正常。 但是我需要将请求参数传递给要返回的特定数量的结果。 当我这样做时,它将创建新的任务列表,而不是列表。 那么该怎么做呢?

我的代码:

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

maxResults是查询参数,而不是请求参数,因此您可以将其放在url中:

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

您还应该考虑使用Java客户端的“任务”接口进行请求。 这可能会更容易一些,因为它可以为您处理url的详细信息:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM