将数据从客户端(计算机)发送到Android Web服务器

时间:2017-08-28 01:26:28

标签: android html webserver client nanohttpd

我正在使用NanoHTTPD Android应用程序在Android APP中创建Web服务器。在此应用程序中,客户端使用智能手机IP连接到服务器。我创建了一个带有textarea的index.html,因此用户可以在网页上写单词。我想知道如何将用户在textarea上键入的文本发送到Android Web Server APP。

这是我的Android代码:

    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);
        }*/

    }

}

这是我的HTML代码:

<!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 个答案:

没有答案
相关问题