7 Jan 2021

How To Create Http's Server in Java Android Programing?


create costom http server
Create Http Server?

Hi, How are you?
I hope that you will be happy.

Today in this particular post we shall know about that How to create Https Server in Android by Java Programing Language? . Now Creating a your own Http's Server, will be easy for you.

<!-- Too much Googly -->

I have Searched too much queries in Google by asking question about How to create costom Https Server in Java Programing?


But I don't get satisfied answer till now. From here I have tried my best. And I got goal of ....

A Goal beyond Creatin a HTTP Server.

I had created this http sever for my app.

How to create http server in Android?

  • MyHttpServer class implements Runnable Interface.

    I declared 3 variables. Names are ...

    1. ServerSocket serverSocket;
    2. Socket socket = null;
    3. String textToshow = " string ";

  • @Overriding run() method,
    Intialising serverSocket = new ServerSocket() .
    Binding InteretSocketAddress .
    serverSocket.bind( new InetSocketAddress("127.0.0.1", 8080) );
public class MyHttpServer implements Runnable

{

  ServerSocket serverSocket;

  Socket socket = null;

  String textToShow = "Ashutosh Kumar  <span style='font-size: 100px;'>女</span>";

  @Override

  public void run()

  {

   try

   {

    serverSocket = new ServerSocket();

    serverSocket.bind( new InetSocketAddress("127.0.0.1", 8080));

    

PrintWriter printWriter;

    

    while(true){

     socket = serverSocket.accept();

     

     printWriter = new PrintWriter( socket.getOutputStream() );

     String response =

                        "<html><head></head>" +

      "<body>" +

      "<h1>" + textToShow + "</h1>" +

      "</body></html>";

      

                 printWriter.print("HTTP/1.0 200" + "\r\n");

     printWriter.print("Content type: text/html" + "\r\n");

     printWriter.print("Content length: " + response.length() + "\r\n");

     printWriter.print("\r\n");

     printWriter.print(response + "\r\n");

     

     printWriter.flush();

     socket.close();

    }

   }

   catch(IOException e)

   {}

   // TODO: Implement this method

  }

}

Final Set up :

Call MyHttpServer in Main thread of android application.
Thread thred = new Thread(new MyHttpServer()); thred.start();

Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE