0
while(isRunning == true) {
    if (SSocket != null) {
        try {
            Socket socket = SSocket.accept();

            PrintStream PStream = new PrintStream(socket.getOutputStream());
            BufferedReader BReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String info = null;
            while ((info = BReader.readLine()) != null) {
                System.out.println("now got " + info);
                if (info.equals("")) {
                    break;
                }
            }

            String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Main></Main>";
            PStream.println("HTTP/1.0 200 OK");
            PStream.println("Content-Type: text/xml");
            PStream.println("Content-Length: " + content.length());
            PStream.println("");
            PStream.println(content);

            PStream.close();
            BReader.close();

            socket.close();

            Thread.sleep(10);
        } catch (Exception e) {

        }
    }
}

This code makes the server display a xml, but when I go to another page (e.g. http://10.0.0.101:39878/otherpage.html), the content is the same. How do I do to change the contents of each page and enter a 404 when it does not exist?

1 Answers1

0

You have to parse the client's request to find out which page is being requested, and then send the appropriate content. The code you showed is not doing any parsing at all. In fact, it is not even reading the client's full request to begin with, only the first line of it, which is not enough. You need to read RFC 2616, which defines the HTTP protocol. And then consider NOT implementing an HTTP server manually, but use a pre-made library instead. See How to create a HTTP server in Android? for some suggestions.

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770