0

I have a simple web page running on a raspberry pi toggle the board's LED with the click of a button. The button executes a javascript which toggles the LED...I essentially want to call this script from an android application's button...

Here is my html:

<html>
<head>
    <script language="javascript">
        function buttonClicked(){
            $.ajax({
                type: "GET",
                url: "phpFiles/toggle.php",
                success : function(){}
              });
            }
    </script>
</head>
<body> just a button calling script </body>

I'm essentially pretty new to this..so I don't know if this is necessarily the best way to go about doing this...

I've tried methods such as this (calling javascript function from an android activity), but I haven't had much luck....

Community
  • 1
  • 1
Matt44
  • 5
  • 4

2 Answers2

1

Just load the toggle.php url in an invisible webview. This should work.

Something like this:

webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("http://walialu.com/my/pi/script/url/toggle.php");  
Walialu
  • 4,096
  • 2
  • 27
  • 29
  • 1
    I spent way too much time trying to get this to work. It's maddening that something so simple took me so long. Thank you though! – Matt44 Dec 07 '14 at 23:32
0

Basically, what you need is an activity with:

  1. A button
  2. Code to do the HTTP GET Request when the button is clicked

I assume that you know how to create the button and how to use the OnClickListener mechanism.

So, what is missing is the way to do your HTTP GET Request, preferably without using a browser or a WebView.

The following page should help you to find the way to do it : https://developer.android.com/training/basics/network-ops/connecting.html

It's a bit more complicated that what you need, but it will show you how to do it in a proper way. (Multi-threading is especially important, your app should never become unresponsive if the server takes a long time to give its response.)

dotpush
  • 428
  • 3
  • 14