-1

I have a PHP site on a Raspberry Pi and would like my Android application to simply go to this site when I click a button and run the code. The code opens a garage door with a relay.

How would I code 'onclick' to go to this PHP site in the background?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Arian
  • 3
  • 2
  • 1
    Possible duplicate of [Make an HTTP request with android](http://stackoverflow.com/questions/3505930/make-an-http-request-with-android) – OneCricketeer Jun 22 '16 at 17:15

1 Answers1

0

This would be a very simple implementation:

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {

            try {
                URL url = new URL("http://");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                try {
                    conn.setReadTimeout(10000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);

                    conn.connect();
                } finally {
                    conn.disconnect();
                }

            } catch (IOException exception) {
                throw new RuntimeException(exception);
            }
        }
    }).start();
}

If you want to do more (such as handling a response from the webpage), read the documentation.

Bryan
  • 14,756
  • 10
  • 70
  • 125