DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:get_webpage_content

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:get_webpage_content [2015/01/20 11:26] (current)
octazid created
Line 1: Line 1:
 +====== Shows how to get Text from a Webpage into your app ======
 +(Code from the DroidScript forum posted by Steve Garman.)
  
 +You can load the sourcecode from any webpage into your app if you change the link in the Example.
 +<code JavaScript GetWebpageContent.js>​
 +var message;
 +
 +//Called when application is started. ​
 +function OnStart() ​
 +
 +    //Create a layout with objects vertically centered. ​
 +    lay = app.CreateLayout( "​linear",​ "​VCenter,​FillXY"​ ); 
 +
 +    //Create a button to send request. ​
 +    btn = app.CreateButton( "Get message",​ 0.3, 0.1 );
 +    btn.SetOnTouch( btn_OnTouch );  ​
 +    lay.AddChild( btn );  ​
 +    //Add layout to app.     
 +    app.AddLayout( lay ); 
 +
 +}    ​
 +
 +//Handle button press. ​
 +function btn_OnTouch()  ​
 +{  ​
 +  //Send request to remote server. ​
 +  var url = "​http://​droidscript.sgarman.net/​message.txt" ​
 +  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 ) 
 +        { 
 +            message = httpRequest.responseText;​
 +            app.Alert( message );
 +        } 
 +        //An error occurred ​
 +        else 
 +           ​app.Alert( "​Error:​ " + httpRequest.status + httpRequest.responseText); ​
 +    } 
 +  app.HideProgress(); ​
 +
 +</​code>​
sample_code/get_webpage_content.txt ยท Last modified: 2015/01/20 11:26 by octazid