Search code examples
javajsoncurlhttp-postgoogle-fit

How to post a complex JSON object to cURL request in java


I wanted to post a JSONObject to https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

AND My JAVA code using HttpPost

 String AccessToken = "ya29.Glv7A7pTaXdXn8EIHlMnGDBMt34OB72bmKowLFxVM7w7MTu7PRqVoBq7eGd0ljMtOk5aDM6y9WkCDdgZ113rzSzXQe6CZV3UXuNvkzWesAl6CfJoA2IZ9U2C9BaG";
    JSONObject jobj = new JSONObject();
    JSONObject jobj3 = new JSONObject();
    jobj3.put("durationMillis", 86400000);
    JSONObject jobj2 = new JSONObject();
    jobj2.put("dataTypeName", "com.google.step_count.delta");
    jobj2.put("dataSourceId", "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps");
    JSONArray jar = new JSONArray();
    jar.add(jobj2);
    jobj.put("aggregateBy", jar);
    jobj.put("bucketByTime", jobj2);
    jobj.put("startTimeMillis", 1487721600000L);
    jobj.put("endTimeMillis", 1487772000000L);

    System.out.println("jobj" + jobj.toJSONString());

    String ApiUrl = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate?access_token=" + AccessToken;
    HttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpPost httpPost = new HttpPost(ApiUrl);
        httpPost.addHeader("Authorization", "Bearer " + AccessToken);
        httpPost.setEntity(new StringEntity(jobj.toJSONString()));
        //sets a request header so the page receving the request
        //will know what to do with it
        httpPost.setHeader("Content-type", "application/json");
        HttpResponse response = httpclient.execute(httpPost);
        System.out.println("\nSending 'GET' request to URL : " + ApiUrl);
        HttpEntity entity = response.getEntity();
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(entity.getContent()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println("line" + line);
        }
    } catch (Exception e) {
        System.out.println("Exception at getDataFromUrl ,error is " + e.getMessage());
    }

and its giving me error

 {"error":{"errors":[{"domain":"global","reason":"invalidArgument","message":"Bad Request"}],"code":400,"message":"Bad Request"}}

referred to https://developers.google.com/fit/scenarios/read-daily-step-total

I am new in this can anybody help me?


Solution

  • It seems you are POSTing incorrect JSON. The JSON generated by your code:

    `

    {
      "endTimeMillis": 1487772000000,
      "startTimeMillis": 1487721600000,
      "bucketByTime": {
        "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
        "dataTypeName": "com.google.step_count.delta"
      },
      "aggregateBy": [
        {
          "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
          "dataTypeName": "com.google.step_count.delta"
        }
      ]
    }
    

    `

    According to the specifications, the bucketByTime should be for example "bucketByTime": { "durationMillis": 86400000 }

    It seems that you are not adding the jobj3 variable in your code to the payload at all, which would contain the correct bucketByTime value.