Hi!
I would need some help, I am quite new on this, I am trying to get some info from a .json file and save it in a variable. All the makeWebRequest seems to work as I print the information with :onreceive but then I do not know how to store it in a variable.
I get the information from the file using makeRequest() that call the onReceive function which saves the information into a public variable "Current_station" and prints the variable. Up to here all is ok.
But then I try to use the variable later, after calling makeRequest() but seems empty. I provide you my code to see if you could help me.
Please, note that I am totally new on this so I might say some things wrongly.
import Toybox.Application;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.System;
class Velib_widgetApp extends Application.AppBase {
var Current_station;
function initialize() {
AppBase.initialize();
}
// onStart() is called on application start up
function onStart(state as Dictionary?) as Void {
}
// set up the response callback function
function onReceive(responseCode, data) {
if (responseCode == 200) {
System.println("Request Successful"); // print success
System.println(data);
Current_station = data["data"]["system_id"];
System.println(Current_station);
}
else {
System.println("Response: " + responseCode); // print response code
}
}
function makeRequest() {
var url = "https://velib-metropole-opendata.smoove.pro/opendata/Velib_Metropole/system_information.json"; // set the url
var params = { // set the parameters
};
var options = { // set the options
:method => Communications.HTTP_REQUEST_METHOD_GET, // set HTTP method
:headers => { // set headers
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON},
// set response type
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
};
// Make the Communications.makeWebRequest() call
Communications.makeWebRequest(url, params, options, method(:onReceive));
}
// onStop() is called when your application is exiting
function onStop(state as Dictionary?) as Void {
}
// Return the initial view of your application here
function getInitialView() as Array<Views or InputDelegates>? {
makeRequest();
System.println(Current_station);
return [ new Velib_widgetView(Current_station) ] as Array<Views or InputDelegates>;
}
}