This shows you the differences between two versions of the page.
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> | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | app.SetOrientation(" | ||
- | |||
- | // | ||
- | existsFile = app.FileExists( "/ | ||
- | |||
- | // | ||
- | if (existsFile == 0) | ||
- | { | ||
- | // | ||
- | app.MakeFolder( "/ | ||
- | // | ||
- | address = " | ||
- | // | ||
- | app.WriteFile( "/ | ||
- | } | ||
- | |||
- | // | ||
- | address = app.ReadFile("/ | ||
- | |||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | // | ||
- | lay.SetBackground( "/ | ||
- | |||
- | // | ||
- | // | ||
- | // | ||
- | laySlide = app.CreateLayout( " | ||
- | laySlide.SetPadding( 0, | ||
- | laySlide.SetBackground( "/ | ||
- | laySlide.SetVisibility( " | ||
- | |||
- | // | ||
- | layVert = app.CreateLayout( " | ||
- | |||
- | // | ||
- | layHori = app.CreateLayout(" | ||
- | layVert.AddChild( layHori); | ||
- | |||
- | // | ||
- | btnHome = app.CreateImage( " | ||
- | btnHome.SetMargins(.0, | ||
- | btnHome.SetOnTouchDown( btnHome_OnTouch ); | ||
- | layHori.AddChild( btnHome ); | ||
- | |||
- | // | ||
- | btnBack = app.CreateImage( " | ||
- | btnBack.SetMargins(.15, | ||
- | btnBack.SetOnTouchDown( btnBack_OnTouch ); | ||
- | layHori.AddChild( btnBack ); | ||
- | |||
- | // | ||
- | btnUp = app.CreateImage( " | ||
- | btnUp.SetOnTouchDown( btnUp_OnTouch ); | ||
- | layVert.AddChild( btnUp ); | ||
- | |||
- | // | ||
- | layHori2 = app.CreateLayout(" | ||
- | layVert.AddChild( layHori2); | ||
- | |||
- | // | ||
- | btnLeft = app.CreateImage( " | ||
- | btnLeft.SetOnTouchDown( btnLeft_OnTouch ); | ||
- | layHori2.AddChild( btnLeft ); | ||
- | |||
- | // | ||
- | btnSelect = app.CreateImage( " | ||
- | btnSelect.SetOnTouchDown( btnSelect_OnTouch ); | ||
- | layHori2.AddChild( btnSelect ); | ||
- | |||
- | // | ||
- | btnRight = app.CreateImage( " | ||
- | btnRight.SetOnTouchDown( btnRight_OnTouch ); | ||
- | layHori2.AddChild( btnRight ); | ||
- | |||
- | // | ||
- | btnDown = app.CreateImage( " | ||
- | btnDown.SetOnTouchDown( btnDown_OnTouch ); | ||
- | layVert.AddChild( btnDown ); | ||
- | |||
- | // | ||
- | layHori3 = app.CreateLayout(" | ||
- | layVert.AddChild( layHori3); | ||
- | |||
- | // | ||
- | btnSave = app.CreateButton( " | ||
- | btnSave.SetOnTouch( btnSave_OnTouch ); | ||
- | laySlide.AddChild( btnSave ); | ||
- | |||
- | // | ||
- | app.SetMenu(" | ||
- | |||
- | // | ||
- | newAddress = app.CreateTextEdit( address, | ||
- | laySlide.AddChild( newAddress ); | ||
- | |||
- | // | ||
- | lay.AddChild( layVert ); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | app.AddLayout(laySlide); | ||
- | |||
- | } | ||
- | |||
- | // | ||
- | function webscript_post() | ||
- | { | ||
- | xhr = new XMLHttpRequest(); | ||
- | xhr.open(' | ||
- | xhr.timeout = setTimeout(" | ||
- | xhr.setRequestHeader(' | ||
- | xhr.send(null); | ||
- | app.Vibrate( " | ||
- | |||
- | } | ||
- | |||
- | // | ||
- | function btnHome_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnBack_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnUp_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnLeft_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnSelect_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnRight_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnDown_OnTouch() | ||
- | { | ||
- | command = ' | ||
- | webscript_post() | ||
- | } | ||
- | |||
- | // | ||
- | function btnIpAddress_OnTouch() | ||
- | { | ||
- | laySlide.Animate( " | ||
- | |||
- | app.Alert( " | ||
- | } | ||
- | |||
- | // | ||
- | function btnFindRoku_OnTouch() | ||
- | { | ||
- | findRoku() | ||
- | } | ||
- | |||
- | //help | ||
- | function help() { | ||
- | app.Alert(" | ||
- | } | ||
- | |||
- | // | ||
- | function OnMenu(item) | ||
- | { | ||
- | if (item == " | ||
- | btnIpAddress_OnTouch() | ||
- | } | ||
- | |||
- | // | ||
- | if (item==" | ||
- | btnFindRoku_OnTouch() | ||
- | } | ||
- | if (item==" | ||
- | help() | ||
- | } | ||
- | } | ||
- | |||
- | // | ||
- | function btnSave_OnTouch() | ||
- | { | ||
- | laySlide.Animate( " | ||
- | address = newAddress.GetText(); | ||
- | app.WriteFile( "/ | ||
- | |||
- | } | ||
- | |||
- | // | ||
- | |||
- | // | ||
- | function findRoku() | ||
- | { | ||
- | |||
- | // | ||
- | ip = app.GetIPAddress(); | ||
- | startNum = ip.split (" | ||
- | ipEndd = Math.floor(startNum[3]/ | ||
- | ipEnd = ipEndd * 50+1 | ||
- | |||
- | // | ||
- | lay_findRoku = app.CreateLayout( " | ||
- | |||
- | // | ||
- | btn = app.CreateButton( " | ||
- | btn.SetMargins( 0, | ||
- | btn.SetOnTouch( btn_OnTouch ); | ||
- | lay_findRoku.AddChild( btn ); | ||
- | |||
- | // | ||
- | txt = app.CreateText( "", | ||
- | txt.SetBackColor( "# | ||
- | txt.SetTextSize( 12 ); | ||
- | lay_findRoku.AddChild( txt ); | ||
- | |||
- | // | ||
- | btnCancel = app.CreateButton( " | ||
- | btnCancel.SetMargins(0, | ||
- | btnCancel.SetOnTouch( loadRemote ); | ||
- | lay_findRoku.AddChild( btnCancel ); | ||
- | |||
- | // | ||
- | app.AddLayout( lay_findRoku ); | ||
- | |||
- | } | ||
- | |||
- | // | ||
- | function loadRemote() | ||
- | { | ||
- | app.RemoveLayout(lay_findRoku); | ||
- | OnStart() | ||
- | } | ||
- | |||
- | // | ||
- | function btn_OnTouch() | ||
- | { | ||
- | |||
- | // | ||
- | ipArray = ip.split (" | ||
- | ipBase = (ipArray[0] + " | ||
- | ipBase += ipEnd; | ||
- | ipBase += ": | ||
- | txt.SetText( " | ||
- | |||
- | // | ||
- | url = " | ||
- | SendRequest( url ); | ||
- | } | ||
- | |||
- | // | ||
- | function SendRequest( url ) | ||
- | { | ||
- | httpRequest = new XMLHttpRequest(); | ||
- | httpRequest.open(" | ||
- | httpRequest.onreadystatechange = function() { HandleReply(httpRequest); | ||
- | httpRequest.send(null); | ||
- | } | ||
- | |||
- | // | ||
- | function HandleReply( httpRequest ) | ||
- | { | ||
- | if( httpRequest.readyState==4 ) | ||
- | { | ||
- | // | ||
- | if( httpRequest.status==200 ) | ||
- | { | ||
- | resTxt = httpRequest.responseText; | ||
- | |||
- | // | ||
- | split1 = resTxt.split('"' | ||
- | split2 = resTxt.split('>' | ||
- | |||
- | // | ||
- | split1Length = split1.length | ||
- | |||
- | // | ||
- | split2Length = split2.length | ||
- | |||
- | // | ||
- | y = 0 | ||
- | idArray = [" | ||
- | for(z=1; | ||
- | { | ||
- | idArray[y] = split1[z] | ||
- | y++ | ||
- | } | ||
- | |||
- | // | ||
- | w = 0 | ||
- | chanArray = [" | ||
- | for(x=2; | ||
- | { | ||
- | chanArray[w] = split2[x] | ||
- | w++ | ||
- | } | ||
- | |||
- | // | ||
- | split3Length = chanArray.length | ||
- | channels ="" | ||
- | for(v=0; | ||
- | { | ||
- | channels = channels + chanArray[v]; | ||
- | } | ||
- | |||
- | // | ||
- | split4 = channels.split('</ | ||
- | split4Length = split4.length | ||
- | t = 0 | ||
- | channelsArray = [" | ||
- | for(s=0; | ||
- | { | ||
- | channelsArray[t] = split4[s] | ||
- | t++ | ||
- | } | ||
- | |||
- | // | ||
- | display =" | ||
- | channelsLength = channelsArray.length | ||
- | for(s=0; | ||
- | { | ||
- | display = display + channelsArray[s] + " | ||
- | } | ||
- | |||
- | //set text | ||
- | txt.SetText(display); | ||
- | |||
- | //write ip | ||
- | writeFile(); | ||
- | |||
- | // | ||
- | setTimeout(" | ||
- | |||
- | } | ||
- | // | ||
- | else | ||
- | { | ||
- | txt.SetText( " | ||
- | |||
- | ipEnd++ | ||
- | if (ipEnd > | ||
- | {ipEnd = 1} | ||
- | setTimeout(" | ||
- | } | ||
- | } | ||
- | |||
- | // | ||
- | function writeFile() | ||
- | { | ||
- | app.WriteFile( "/ | ||
- | app.Vibrate( " | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | 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. |