[Sample App] Using HTTP GET to fetch data from a web server

439 views
Skip to first unread message

Dave Smart

unread,
Apr 30, 2014, 7:59:15 PM4/30/14
to androi...@googlegroups.com
Here's a sample app which does an HTTP Get to fetch data from a public web service:-

--------------------------------------------------------

//This sample demonstrates getting a list of UK place names
//within a post code area, using the free geonames.org web service.

//Called when application is started.
function OnStart()
{
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );    

    //Create an text edit box for postcode entry. 
    edt = app.CreateTextEdit( "CB1", 0.8 ); 
    lay.AddChild( edt ); 

    //Create a button to send request.
    btn = app.CreateButton( "Get Places", 0.3, 0.1 ); 
    btn.SetMargins( 0, 0.05, 0, 0 ); 
    btn.SetOnTouch( btn_OnTouch ); 
    lay.AddChild( btn ); 

    //Create a text label to show results.
    txt = app.CreateText( "", 0.8, 0.6, "Left,Multiline" ); 
    txt.SetBackColor( "#ff222222" ); 
    txt.SetTextSize( 12 );
    lay.AddChild( txt );
    
    //Add layout to app.    
    app.AddLayout( lay );

}   

//Handle button press.
function btn_OnTouch() 
  //Send request to remote server.
  var postCode = edt.GetText();
        + "postalcode=" + postCode + "&country=UK&username=androidscript";
  SendRequest( url  );
}

//Send an http get request.
function SendRequest( url )
{
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };  
    httpRequest.open("GET", url, true);
    httpRequest.send(null);
    
    app.ShowProgress( "Loading..." );
}

//Handle the server's reply (a json object).
function HandleReply( httpRequest )
{
    if( httpRequest.readyState==4 )
    {
        //If we got a valid response.
        if( httpRequest.status==200 )
        {
            txt.SetText( "Response: " + httpRequest.status + httpRequest.responseText);
        }
        //An error occurred
        else
            txt.SetText( "Error: " + httpRequest.status + httpRequest.responseText);
    }
  app.HideProgress();
}


Netpower8

unread,
Oct 30, 2015, 1:11:39 PM10/30/15
to AndroidScript
this sample program for api data fetching... can this be implemented for BOX (www.box.com) ?

thanks for the reply

Netpower8

unread,
Oct 31, 2015, 10:33:14 AM10/31/15
to AndroidScript
is there a sample program for api data fetching ???... can this be implemented for BOX (www.box.com) ?

like downloading or uploading files to a box acct. (www.box.com)

Mark Allemang

unread,
Apr 21, 2025, 8:44:24 AMApr 21
to DroidScript
this is an example HTTP GET to fetch data....a postal code lookup.
If you want to do this with www.box.com ...just go here for info:  https://developer.box.com/reference/
Reply all
Reply to author
Forward
0 new messages