Hi there,
Tonight I concentrated on the app GUI and the target was to get valid data parsed from the JSON (that eventually will come from Fitbit API), and to view the weight and the date of the latest measurement on the view labels.
The function below is the callback that is called when the HTTP GET request is finished. Here we verify that the response code is correct. If yes, the handler function is notified of the available response and is provided the obtained data as parameter.
function onReceive(responseCode, data) { System.println("onReceive(): responseCode: " +responseCode +", data: " +data); if (responseCode == 200) { notify.invoke(data); } else { notify.invoke("Failed to load\nError: " + responseCode.toString()); } }
I discussed already in the part 2 the Fitbit API for weight, so have a look at that post, if you want to refresh your memory. The data handler function in the app reads two fields from the JSON, the weight measure and the date of the measurement. This current implementation does not take into account multiple measurements per day etc., so there’s still a lot to do after the basic functionality is there. Basically there only should occur the another option (JSON in form of a Dictionary (key – value pairs)). In this case there only is one value in the array, so we can use it directly in the variable “internalValues“. From the array we in turn fetch the actual figure for the weight and the date of the measurement. I have previously specified the member variables mWeight and mMeasurementDate and those are used as values for the labels.
function onReceive(json) { if (json instanceof Lang.String) { mWeight = json; } else if (json instanceof Lang.Dictionary) { var keys = json.keys(); var values = json.values(); for (var i = 0; i < keys.size(); i++ ) { System.println(keys[i]); System.println(values[0]); var internalValues = values[0]; if (internalValues instanceof Lang.Array) { var weightValue = internalValues[0]["weight"]; var weightString = weightValue.format("%.01f"); var measurementDate = internalValues[0]["date"]; mWeight = weightString; mMeasurementDate = measurementDate; } } } Ui.requestUpdate(); }
Now I’m too exhausted to continue, but I’ll go instead to bed, and continue next time with the app. We are still missing at least the OAuth2 authentication and binding the whole app to the Fitbit API. Until next time!