Um Daten von einem Remote-Server zu laden kann, wie in Javascript üblich, das
XMLHttpRequest -Objekt für asynchrone Requests (Ajax) verwendet werden. Folgendes
DroidScript -Skript kann als Basis für eigene Entwicklungen genutzt werden:
download DEVjsonTestXHR.js
"use strict";
var lay,txt;
var txtIp, txtDate, txtHd;
function OnStart()
{
lay = app.CreateLayout( "linear", "left,FillXY" );
txt = app.CreateText('XMLHttpRequest Test ',-1,-1,'html');
txt.SetTextSize(20);
lay.AddChild( txt );
txtIp = app.CreateText('',-1,-1,'html' );
lay.AddChild( txtIp );
txtDate = app.CreateText('',-1,-1,'html,multiline,left');
lay.AddChild(txtDate);
txtHd = app.CreateText('',-1,-1,'html,multiline,left');
lay.AddChild(txtHd);
app.AddLayout( lay );
testXHR();
}
function rq(url,cb){
var xhr = new XMLHttpRequest();
xhr.open("GET",url,true);
xhr.onreadystatechange=function(){
if(xhr.readyState===4) cb(JSON.parse(xhr.responseText));
}
xhr.send();
};
function getIP(cb){
rq('http://ip.jsontest.com/',cb);
}
function getHeaders(cb){
rq("http://headers.jsontest.com/",cb);
}
function getDate(cb){
rq("http://date.jsontest.com/",cb);
}
function testXHR(){
getIP(function(o){
txtIp.SetHtml("IP: "+o.ip);
});
getDate(function(o){
txtDate.SetHtml("Date: "+o.date+"Time: "+o.time);
});
getHeaders(function(o){
var s="";
Object.keys(o).forEach(function(n){
s+=""+n+": "+o[n]+" ";
});
txtHd.SetHtml(s);
});
}
Keine Kommentare:
Kommentar veröffentlichen