This tutorial present HTTP network connection via the
HTTP javascript library.
HTTP javascript library offers same
functionality than XMLHttpRequest.
In fist time, we send a request to Google and we display result in console.
To send request, we use the get method (GET HTTP request).
Get method : void get (String url, Function callback, String encoding)
url is the google URL (http://google.com).
callback is a function to process request result.
encoding is optional (by default UTF-8). We use the default value.
First example code:
In previous example, we send a HTTP request and display the result.
Detail of the callback function:
In next example, with callback function, we verify the response:
Try to change URL to verify if you catch an error (example, use URL 'http://goolgle.com').
To optimize network access, MeMo offers cache features.
See cache part, in advanced chapter.
Cache example:
To process XML (web service, ...) or JSON responses, Mobile Widgets offers javascript libraries.
See JSON library documentation for process JSON result.
See XML library documentation for process XML result.
Example: Load JSON data and process JSON result:
Example: Load XML data and process XML result:
XML libary can load data without use HTTP library, via the open method.
Method: XML.open (url, XML.URL, callback);
Callback function: parseXml(url, responseCode, id, now)
With id the identity of XML result.
Previous example without use HTTP library: