Send data from a client (computer) to an Android Web Server

305 views Asked by At

I am using the NanoHTTPD Android App to create a Web Server in an Android APP. In this app, the client connect to the server using the smartphone IP. I created an index.html with a textarea, so the user could write words on the web page. I was wondering how can i send the text that the user has typed on the textarea to the Android Web Server APP.

Here is my Android code:

    import android.content.Context;
    import android.net.wifi.WifiInfo;
    import android.os.Bundle;
    import java.io.IOException;
    import java.util.Map;
    import android.app.Activity;
    import android.net.wifi.WifiManager;
    import android.util.Log;
    import android.widget.TextView;
    import fi.iki.elonen.NanoHTTPD;
    import android.os.Environment;
    import java.io.*;
    import java.util.*;

public class MainActivity extends Activity {
    private WebServer server;
    Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        server = new WebServer();
        TextView EnderecoIp = (TextView)findViewById(R.id.TxtIp);

        try {

            server.start();
            WifiManager wifiMan = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInf = wifiMan.getConnectionInfo();
            int ipAddress = wifiInf.getIpAddress();
            String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
            System.out.println(ip);

            EnderecoIp.setText("Endereço de IP: " + ip + ":8080");

        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");
    }

    // DON'T FORGET to stop the server
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (server != null)
            server.stop();
    }

    private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method,
                              Map<String, String> header,
                              Map<String, String> parameters,
                              Map<String, String> files) {
            String answer = "";

            try {
                // Open file from SD Card
                File root = Environment.getExternalStorageDirectory();
                FileReader index = new FileReader(root.getAbsolutePath() + "/Pictures/index.html");

                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    answer += line;
                }
                reader.close();

            } catch(IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }



            return new NanoHTTPD.Response(answer);
        }

        /*@Override
        public Response serve(IHTTPSession session) {

            Map<String, String> files = new HashMap<String, String>();
            Method method = session.getMethod();
            if (Method.PUT.equals(method) || Method.POST.equals(method)) {
                try {
                    session.parseBody(files);
                } catch (IOException ioe) {
                    return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
                } catch (ResponseException re) {
                    return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
                }
            }
            // get the POST body
            String postBody = session.getQueryParameterString();
            // or you can access the POST request's parameters
            String postParameter = session.getParms().get("parameter");

            return new Response(postBody); // Or postParameter.
        }*/


      /*  @Override
        public Response serve(IHTTPSession session) {
            String answer = "";
            Map<String, String> files = new HashMap<String, String>();
            Method method = session.getMethod();
            if (Method.PUT.equals(method) || Method.POST.equals(method)) {
                try {
                    session.parseBody(files);
                } catch (IOException ioe) {
                    return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
                } catch (ResponseException re) {
                    return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
                }
            }
            try {
                // Open file from SD Card
                File root = Environment.getExternalStorageDirectory();
                FileReader index = new FileReader(root.getAbsolutePath() + "/Pictures/index.html");
                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    answer += line;
                }
                reader.close();

            } catch(IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }



            // get the POST body
            String postBody = session.getQueryParameterString();
            // or you can access the POST request's parameters
            String postParameter = session.getParms().get("parameter");

            System.out.println("Resposta1 = " + answer);
            System.out.println("Resposta2 = " + postBody);
            System.out.println("Resposta3 = " + postParameter);


            return new NanoHTTPD.Response(answer);
        }*/

    }

}

Here is my html code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>titulo 1</title>
</head>
<body>
<h1> Projeto IC </h1>
<p>  Digite os comandos desejados </p>
    <!-- Conteúdo -->



<form action="salvar_dados.txt" method="post">
    <textarea cols=50 rows=8>
        TEXTO AQUI!
    </textarea>
    <input type="checkbox">Checkbox <br/>
    <input type="radio">Radio button <br/>
    <input type="range"> <br/>
    <input type="submit" value="Enviar">

</form>

</body>
</html>
0

There are 0 answers