MFC_Starter MFC_Starter - 19 days ago 6
Android Question

Android studio http server

I want to use an android application to control a http server.
I implemented a wifi shield with some output pins.

If I browse the website 192.168.xxx.xx/wifishield/socketOn, one of the output of the wifi shield is high, if I browse the website 192.168.xxx.xx/wifishield/socketOff, the output is low.

Do you have any idea how I can implement two buttons in an application which "browse" this websites to toggle the output via a button?

Or is there another good idea to control the output of the wifi shield via an android application?

Thanks for your help :)

Answer

This should work but you need this permission in Manifest <uses-permission android:name="android.permission.INTERNET" /> and library called Volley

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_profile);

    Button on = (Toolbar) findViewById(R.id.button1);
    Button off = (Toolbar) findViewById(R.id.button2);

    on.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            toggleServer(true);
        });

    on.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            toggleServer(false);
        });

}

private void toggleServer(boolean on) {
    RequestQueue queue = Volley.newRequestQueue(this);
    String insertURL;
    if(on){
        insertURL = 192.168.xxx.xx/wifishield/socketOn;
    }else
        insertURL = 192.168.xxx.xx/wifishield/socketOff;

    StringRequest request = new StringRequest(Request.Method.GET, insertURL, 
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.d("toggleServer", "Everything is OK");
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            prefsEdit.putString(SENT_TOKEN_TO_SERVER, "false").apply();
            Log.e("toggleServer", "Error occured");
        }
    }) {

    };

    queue.add(request);

}
Comments