Monday 1 September 2014

HTTP POST and the Web IDE

I've been playing with http post whilst travelling around a bit. Just getting the feel so I can use it in a project I have coming up.

It occurred to me that there was a way I could be even more lazy than currently.

So I set about writing a little app that will enable me to bookmark the server for the Web IDE, even though the ip address changes occasionally.

I just open the app, hit Ok, wait a couple of second for the button to rename itself to Exit, then hit it again.

The bookmark on my PC then opens a page with a link to the server on my tablet.

Give it a day or two and I'll probably cut out the link and go for a redirect instead. The only problem with that is it makes the initial bookmarking a bit tricky.

The code I use is reproduced below. It points to a different url than I use, so you are welcome to try it.

In the highly unlikely event that 2 people try to use it at the same time, you may not get the right link.
//public url
var path = "http://androidscript.sgarman.net/webide/" 
var pthfile = "test3.php"
//globals
var txt,btn;

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

    //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 ); 

    //Create a button to send request. 
    btn = app.CreateButton( "Ok", 0.3, 0.1 );  
    btn.SetMargins( 0, 0.05, 0, 0 );  
    btn.SetOnTouch( btn_OnTouch );  
    lay.AddChild( btn );  
  
    //Add layout to app.     
    app.AddLayout( lay ); 
}    

//Handle button press. 
function btn_OnTouch()  
{  
  if(btn.GetText() == "Exit"){
     app.Exit();
  } else{
  //Send request to remote server. 
  var url = path + pthfile;
  var data = "url=http://" + app.GetIPAddress() + ":8088/edit"; 
  SendRequest( url , data ); 
  }
} 

//Send an http post request. 
function SendRequest( url, params ) 
{ 
    var httpRequest = new XMLHttpRequest(); 
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); }; 
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-type",
        "application/x-www-form-urlencoded"); 
    httpRequest.setRequestHeader("Content-length", params.length); 
    httpRequest.setRequestHeader("Connection", "close"); 
     
    httpRequest.send(params); 
     
    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( httpRequest.responseText + "\n\n"+ 
               "Please point your PC browser at\n"+ path ); 
            btn.SetText("Exit");
        } 
        //An error occurred 
        else{
          txt.SetText( "Error: " + httpRequest.status + httpRequest.responseText);
        }
    } 
    app.HideProgress(); 
} 

1 comment:

  1. The app suggest that you create a bookmark on your PC to
    http://androidscript.sgarman.net/webide/

    If you then go in and edit the bookmark, changing the URL to
    http://androidscript.sgarman.net/webide/?redir=true
    It should automatically redirect to the web IDE

    ReplyDelete