oauthP5 / Openpaths: How to convert API response to JSONArray?
in
Contributed Library Questions
•
2 months ago
Hi! I´m a student working on my thesis project, and using openpaths. I'm having a problem when trying to use my data that is returned from the API. On the "blprintOpenPathsExample.pde" example there is an attempt to convert the response.getBody() in to a JSONArray. Wich gives me an error "JSONArray is ambiguos". After looking at the javadocs, I found out that response.getBody() returns a String. So, as a work around, I'm saving dynamically that string as a local "data.json" file and then load it back as a JSONArray. But I would like to convert it directly without having to save a local file. I´m sure its possible... Am I doing something wrong? Can someone help me? Would be very appreciated!
Best regards!
Here is my code:
- final String ACCESS = "";
- final String SECRET = "";
- final String URL = "https://openpaths.cc/api/1";
- void openPaths()
- {
- OAuthService service = new ServiceBuilder()
- .provider(OpenPathsApi.class)
- .apiKey(ACCESS)
- .apiSecret(SECRET)
- .build();
- OAuthRequest request = new OAuthRequest(Verb.GET, URL);
- Token token = new Token("", "");
- service.signRequest(token, request);
- request.addQuerystringParameter("start_time", String.valueOf(System.currentTimeMillis() / 1000 - 30*24*60*60));
- request.addQuerystringParameter("end_time", String.valueOf(System.currentTimeMillis() / 1000));
- Response response = request.send();
- //println(response.getBody());
- String[] receivedJSONBody = { response.getBody() };
- //println(receivedJSONBody);
- saveStrings("openpaths.json", receivedJSONBody);
- JSONArray loadedJSONObjects = loadJSONArray("openpaths.json");
- for (int i=0; i<loadedJSONObjects.size(); i++)
- {
- JSONObject location = loadedJSONObjects.getJSONObject(i);
- double longitude = location.getFloat("lon");
- double latitude = location.getFloat ("lat");
- double altitude = location.getFloat ("alt");
- long timeStamp = location.getInt ("t");
- println("Longitude: "+longitude);
- println("Latitude: "+latitude);
- println("Altitude: "+altitude);
- println("Time Stamp: "+timeStamp);
- println("");
- }
- }
1