User Tools

Site Tools


sample_code:sample_xmlhttprequest

Differences

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

Link to this comparison view

sample_code:sample_xmlhttprequest [2014/12/22 01:10]
smpeters [XMLHttpRequest Example]
sample_code:sample_xmlhttprequest [2014/12/22 10:08]
Line 1: Line 1:
-Sample program that demonstrates how to use XMLHttpRequest POST and GET. The example is a simple roku remote app i wrote for younger kids to use. 
  
-====== XMLHttpRequest Example ====== 
- 
-<file java kidsroku.js> 
-//Called when application is started. 
-function OnStart() 
-{ 
-     //keep in portrait orientation 
-     app.SetOrientation("Portrait"); 
- 
-//Check if roku.pref exists 
-existsFile = app.FileExists( "/sdcard/kidsroku/roku.pref" ); 
- 
-//check for for file existance 
-if (existsFile == 0)  
-{ 
- //create folder for preferences 
- app.MakeFolder( "/sdcard/kidsroku/" ); 
- //default value 
- address = "http://172.16.20.84:8060/"; 
- //write preference file 
- app.WriteFile( "/sdcard/kidsroku/roku.pref", address ); 
-} 
- 
-// read file roku.pref 
-address = app.ReadFile("/sdcard/kidsroku/roku.pref"); 
- 
-    //Create a layout with objects vertically centered. 
-    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );     
-    //set background  
-    lay.SetBackground( "/Sys/Img/BlackBack.png" );  
- 
-    //Create a layout we can slide over the main layout.  
-    //(This hidden layout is actually on top of the main  
-    //layout, it just appears to slide from the left)  
-    laySlide = app.CreateLayout( "Linear", "FillXY" );  
-    laySlide.SetPadding( 0, 0.1, 0, 0 );   
-    laySlide.SetBackground( "/Sys/Img/GreenBack.png" );  
-    laySlide.SetVisibility( "Hide" ); 
- 
-    //Create Verticle sub-layout for buttons. 
-    layVert = app.CreateLayout( "Linear", "Verticle" );     
- 
-    //Create Horizontal sub-layout for buttons 
-    layHori = app.CreateLayout("linear", "Horizontal"); 
-    layVert.AddChild( layHori); 
-     
-    //Create Home button. 
-    btnHome = app.CreateImage( "Img/home.png", .25, -1 ); 
-    btnHome.SetMargins(.0,.01,.15,.08); 
-    btnHome.SetOnTouchDown( btnHome_OnTouch ); 
-    layHori.AddChild( btnHome ); 
-     
-    //Create Back button. 
-    btnBack = app.CreateImage( "Img/back.png", .25, -1 ); 
-    btnBack.SetMargins(.15,.01,.0,.08); 
-    btnBack.SetOnTouchDown( btnBack_OnTouch ); 
-    layHori.AddChild( btnBack ); 
-     
-    //Create Up button. 
-    btnUp = app.CreateImage( "Img/up.png",.35,-1 ); 
-    btnUp.SetOnTouchDown( btnUp_OnTouch ); 
-    layVert.AddChild( btnUp ); 
-     
-    //Create horizontal layout2 for buttons 
-    layHori2 = app.CreateLayout("linear", "Horizontal"); 
-    layVert.AddChild( layHori2); 
-     
-    //Create Left button. 
-    btnLeft = app.CreateImage( "Img/left.png",.35,-1 ); 
-    btnLeft.SetOnTouchDown( btnLeft_OnTouch ); 
-    layHori2.AddChild( btnLeft ); 
-     
-    //Create Select button. 
-    btnSelect = app.CreateImage( "Img/select.png",.35,-1 ); 
-    btnSelect.SetOnTouchDown( btnSelect_OnTouch ); 
-    layHori2.AddChild( btnSelect ); 
-     
-    //Create Right button.l 
-    btnRight = app.CreateImage( "Img/right.png",.35,-1 ); 
-    btnRight.SetOnTouchDown( btnRight_OnTouch ); 
-    layHori2.AddChild( btnRight ); 
-     
-    //Create Down button. 
-    btnDown = app.CreateImage( "Img/down.png",.35,-1 ); 
-    btnDown.SetOnTouchDown( btnDown_OnTouch ); 
-    layVert.AddChild( btnDown ); 
-     
-    //Create horizontal layout3 for buttons 
-    layHori3 = app.CreateLayout("linear", "Horizontal"); 
-    layVert.AddChild( layHori3); 
-    
-    //Create button and add to main layout.  
-    btnSave = app.CreateButton( "Save", 0.3, 0.06, "gray" );  
-    btnSave.SetOnTouch( btnSave_OnTouch );  
-    laySlide.AddChild( btnSave ); 
- 
-    //Create menu 
-    app.SetMenu("IP Address,Find Roku,Help"); 
- 
-    //Create text edit 
-    newAddress = app.CreateTextEdit( address, 0.8, 0.3 );  
-    laySlide.AddChild( newAddress ); 
- 
-    //Add verticle layout to main layout. 
-    lay.AddChild( layVert ); 
-     
-    //Add layout to app.     
-    app.AddLayout( lay ); 
-    app.AddLayout(laySlide); 
- 
-} 
- 
- //webscript post 
-function webscript_post() 
-{ 
-    xhr = new XMLHttpRequest(); 
-    xhr.open('POST', address + command,true); 
-    xhr.timeout = setTimeout("xhr.abort()", 300); 
-    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
-    xhr.send(null); 
-    app.Vibrate( "0,20" ); 
- 
-} 
- 
-//Called when user touches Home button. 
-function btnHome_OnTouch() 
-{ 
-command = 'keypress/home' 
-webscript_post() 
-} 
- 
-//Called when user touches Back button. 
-function btnBack_OnTouch() 
- 
-command = 'keypress/back' 
-webscript_post() 
-} 
- 
-//Called when user touches up button. 
-function btnUp_OnTouch() 
-{ 
-command = 'keypress/up' 
-webscript_post() 
-} 
- 
-//Called when user touches Left button. 
-function btnLeft_OnTouch() 
-{ 
-command = 'keypress/left' 
-webscript_post() 
-} 
- 
-//Called when user touches Select button. 
-function btnSelect_OnTouch() 
-{ 
-command = 'keypress/select' 
-webscript_post() 
-} 
- 
-//Called when user touches Right button. 
-function btnRight_OnTouch() 
-{ 
-command = 'keypress/right' 
-webscript_post() 
-} 
- 
-//Called when user touches Down button. 
-function btnDown_OnTouch() 
-{ 
-command = 'keypress/down' 
-webscript_post()       
- 
- 
-//Called when user touches IP Adress menu item. 
-function btnIpAddress_OnTouch() 
-{ 
-laySlide.Animate( "SlideFromLeft" ); 
- 
-    app.Alert( "http://172.16.20.56:8060/", "make sure to use this format " ); 
-} 
- 
-//Called when user touches Find Roku menu item 
-function btnFindRoku_OnTouch() 
-{ 
-   findRoku() 
-} 
- 
-//help 
-function help() { 
-   app.Alert("A simple roku remote for kids or anyone looking for a basic easy to use remote"); 
-} 
- 
-//Called when user touches menu item Ip button 
-function OnMenu(item) 
-{ 
-if (item == "IP Address") { 
-    btnIpAddress_OnTouch() 
-} 
- 
-// Called when user touches menu item Find Roku 
-if (item=="Find Roku") { 
-    btnFindRoku_OnTouch() 
-} 
-if (item=="Help") { 
-   help() 
-} 
-} 
- 
-//Called when user touches save button 
-function btnSave_OnTouch() 
-{ 
-laySlide.Animate( "SlideToLeft" ); 
-address = newAddress.GetText(); 
-app.WriteFile( "/sdcard/kidsroku/roku.pref", address ); 
- 
-} 
-  
-// ***************************************************** 
- 
-//Search network for roku 
-function findRoku()  
- 
- 
-//Get ip of device 
-    ip = app.GetIPAddress(); 
-    startNum = ip.split ("."); 
-    ipEndd = Math.floor(startNum[3]/50); 
-    ipEnd = ipEndd * 50+1 
- 
-//Create a layout with objects vertically centered.  
-    lay_findRoku = app.CreateLayout( "linear", "VCenter,FillXY" ); 
- 
-//Create a button to send request.  
-    btn = app.CreateButton( "Find Roku", 0.9, 0.1 );   
-    btn.SetMargins( 0, 0, 0, 0 );   
-    btn.SetOnTouch( btn_OnTouch );   
-    lay_findRoku.AddChild( btn );   
- 
-//Create a text label to show results.  
-    txt = app.CreateText( "", 0.9, 0.8, "Left,Multiline" ) 
-    txt.SetBackColor( "#ff222222" );   
-    txt.SetTextSize( 12 );  
-    lay_findRoku.AddChild( txt );  
- 
-// Create a button to cancel request 
-    btnCancel = app.CreateButton( "Cancel", 0.9, 0.1); 
-    btnCancel.SetMargins(0, 0, 0, 0); 
-    btnCancel.SetOnTouch( loadRemote ); 
-    lay_findRoku.AddChild( btnCancel ); 
-      
-//Add layout to app.      
-    app.AddLayout( lay_findRoku );  
- 
-}     
- 
-//Load roku remote 
-function loadRemote() 
-{ 
-app.RemoveLayout(lay_findRoku); 
-OnStart() 
-} 
- 
-//Handle button press.  
-function btn_OnTouch()   
-{   
- 
-//find device 
-  ipArray = ip.split ("."); 
-  ipBase = (ipArray[0] + "." + ipArray[1] + "." + ipArray[2] + "."); 
-  ipBase += ipEnd; 
-  ipBase += ":8060/" 
-  txt.SetText( "Checking..." + ipBase); 
-   
-//Send request to roku 
-  url = "http://" + ipBase + "/query/apps"; 
-  SendRequest( url  );  
- 
- 
-//Send an http get request.  
-function SendRequest( url )  
- 
-    httpRequest = new XMLHttpRequest();     
-    httpRequest.open("GET", url, true);  
-    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); }; 
-     httpRequest.send(null);    
-} 
- 
-//Handle the roku's reply 
-function HandleReply( httpRequest )  
- 
-   if( httpRequest.readyState==4 )  
-    {  
-        //If we got a valid response.  
-        if( httpRequest.status==200 )  
-        {  
-   resTxt = httpRequest.responseText; 
- 
-//split returned xml 
-   split1 = resTxt.split('"'); 
-   split2 = resTxt.split('>'); 
- 
-//get length of array split1 
-split1Length = split1.length 
- 
-//get length of array split2 
-split2Length = split2.length 
- 
-//build array of id's 
-   y = 0 
-   idArray = ["0"] 
-for(z=1; z<split1Length; z=z+4) 
-   { 
-   idArray[y] = split1[z] 
-   y++ 
-   } 
- 
-//build array of channels 
-w = 0 
-chanArray = ["0"] 
-for(x=2; x<split2Length; x=x+2) 
-{ 
-chanArray[w] = split2[x] 
-w++ 
-} 
- 
-//trim array for channels 
-split3Length = chanArray.length 
-channels ="" 
-for(v=0; v<split3Length; v++) 
-   { 
-   channels = channels + chanArray[v]; 
-   } 
- 
-//split xml 
-split4 = channels.split('</app'); 
-split4Length = split4.length 
-t = 0 
-channelsArray = ["0"] 
-for(s=0; s<split4Length; s++) 
-   { 
-   channelsArray[t] = split4[s] 
-   t++ 
-   } 
- 
-//view channels 
-display ="Configuration Update Sucessful\n\nRoku IP:" +  ipBase + "\n\nYour Channels:\n\n" 
-channelsLength = channelsArray.length 
-for(s=0; s<channelsArray.length; s++) 
-   { 
-   display = display + channelsArray[s] + "\n" 
-   } 
- 
-//set text 
-txt.SetText(display); 
- 
-//write ip 
-writeFile(); 
- 
-//Load Roku Remote 
-setTimeout("loadRemote()", 3000); 
- 
-        }  
-   //An error occurred  
-   else  
-   { 
-            txt.SetText( "Error: " + httpRequest.status + httpRequest.responseText);  
-     
-  ipEnd++ 
-  if (ipEnd > 254) 
-  {ipEnd = 1} 
-  setTimeout("btn_OnTouch()", 250); 
-} 
-} 
- 
-//write file for ip address 
-function writeFile() 
-{ 
-app.WriteFile( "/sdcard/kidsroku/roku.pref", "http://" + ipBase ); 
-  app.Vibrate( "0,100,30,100,50,300" ); 
-} 
- 
- 
-</file> 
- 
-This file uses .png files as buttons that will need to created of modified to use standard buttons...i tried to upload but don't have the permission to upload pictures. 
sample_code/sample_xmlhttprequest.txt · Last modified: 2014/12/22 10:08 (external edit)