17 Apr

Java Android application to send http commands via htaccess for remote control

This is a quick one.  In our recent post Remote Control Raspberry via Apache2 Web Server we explained how to receive a command on an apache2 web server. And in JavaScript function to send http commands via htaccess for remote control we demonstrated how to send such a control command via JavaScrip. Now here is the minimal Java Android application to send http commands via htaccess for remote control e.g. your raspberry pi. So when you push a button on you android phone, your raspbery pi at home does whatever you want him to do.

In the first above mentions post we created a web server reacting on calls like

https://HOSTNAME/control_page.php?my_variable=my_value

But you have to input username and password for the htaccess security in a popup. So doing this in one command looks like this:

https://USER:PASSWORD@HOSTNAME/control_page.php?my_variable=my_value

This in great. Now we want to apply Java to send the command above to our server. Here is how it’s done in a minimal app:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

public class HomeActivity extends Activity {

    private Button btnSendCmd;
    private TextView tv_test;

    private void trustEveryone() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }});
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new X509TrustManager(){
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }}}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(
                    context.getSocketFactory());
        } catch (Exception e) { // should never happen
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        trustEveryone();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);    

        btnSendCmd = (Button) findViewById(R.id.btnSendCmd);
        tv_test = (TextView) findViewById(R.id.tv_test);

        btnSendCmd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PostClass().execute(new String[]{"https://HOSTNAME/control_page.php?my_variable=my_value"});
            }
        });

    }

    private class PostClass extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            try {

                final TextView outputView = (TextView) findViewById(R.id.postOutput);
                URL url = new URL(params[0]);

                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                String urlParameters = "";
                connection.setRequestMethod("POST");

                connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

                final String basicAuth = "Basic " + Base64.encodeToString("USER:PASSWORD".getBytes(), Base64.URL_SAFE);
                connection.setRequestProperty("Authorization", basicAuth);

                connection.setDoOutput(true);
                DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
                dStream.writeBytes(urlParameters);
                dStream.flush();
                dStream.close();
                int responseCode = connection.getResponseCode();
                final StringBuilder output = new StringBuilder("Request URL " + url);
                output.append(System.getProperty("line.separator") + "Request Parameters " + urlParameters);
                output.append(System.getProperty("line.separator") + "Response Code " + responseCode);
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                final StringBuilder responseOutput = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    responseOutput.append(line);
                }
                br.close();

                output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());

                HomeActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        outputView.setText(output);
                        ;

                    }
                });

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }
}

Explanation:

  • The function trustEveryone() is  to handle  self signed SSL CA.
  • The http(s) post is send in a PostClass obkect as asynchronous background task and the results were written to the tv_test textview.
  • Authentication for htaccess ois realized by a defined header like
    final String basicAuth = "Basic " + Base64.encodeToString("USER:PASSWORD".getBytes(), Base64.URL_SAFE);

    For your implementation you have to change  USER and PASSWORD accordingly.

If you like the script or need some help on adapting this to your needs, please leave a comment.

Leave a Reply

Read previous post:
JavaScript function to send http commands via htaccess for remote control

In our recent post Remote Control Raspberry via Apache2 Web Server we explained how to receive a command on an...

Close