Simple Http Server Running On Android

Posted on

It’s my former do “Implement uncomplicated HTTP server running on Android“. Somebody written report it’s non work, in addition to thence I re-try it on Android Studio. Actually the code is same every minute before, in addition to travel every minute expected. Please notice both the server in addition to customer produce got to connect inwards the same WiFi network.


Updated@2015-11-26:
This code NOT travel on Android 5, may live all 5+ version.
Fixed, cite adjacent postal service “socket.getInetAddress() supply goose egg on Android 5“.


Work on Xiaomi Redmi 2, running Android 4.4.4


MainActivity.java

package com.blogspot.android_er.androidhttpserver;  import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import android.widget.TextView;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration;  world shape MainActivity extends AppCompatActivity {      EditText welcomeMsg;     TextView infoIp;     TextView infoMsg;     String msgLog = "";      ServerSocket httpServerSocket;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          welcomeMsg = (EditText) findViewById(R.id.welcomemsg);         infoIp = (TextView) findViewById(R.id.infoip);         infoMsg = (TextView) findViewById(R.id.msg);          infoIp.setText(getIpAddress() + ":"                 + HttpServerThread.HttpServerPORT + "\n");          HttpServerThread httpServerThread = novel HttpServerThread();         httpServerThread.start();     }      @Override     protected void onDestroy() {         super.onDestroy();          if (httpServerSocket != null) {             drive {                 httpServerSocket.close();             } grab (IOException e) {                 e.printStackTrace();             }         }     }      soul String getIpAddress() {         String ip = "";         drive {             Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface                     .getNetworkInterfaces();             field (enumNetworkInterfaces.hasMoreElements()) {                 NetworkInterface networkInterface = enumNetworkInterfaces                         .nextElement();                 Enumeration<InetAddress> enumInetAddress = networkInterface                         .getInetAddresses();                 field (enumInetAddress.hasMoreElements()) {                     InetAddress inetAddress = enumInetAddress.nextElement();                      if (inetAddress.isSiteLocalAddress()) {                         ip += "SiteLocalAddress: "                                 + inetAddress.getHostAddress() + "\n";                     }                  }              }          } grab (SocketException e) {             // TODO Auto-generated grab block             e.printStackTrace();             ip += "Something Wrong! " + e.toString() + "\n";         }          supply ip;     }      soul shape HttpServerThread extends Thread {          static lastly int HttpServerPORT = 8888;          @Override         world void run() {             Socket socket = null;              drive {                 httpServerSocket = novel ServerSocket(HttpServerPORT);                  while(true){                     socket = httpServerSocket.accept();                      HttpResponseThread httpResponseThread =                             novel HttpResponseThread(                                     socket,                                     welcomeMsg.getText().toString());                     httpResponseThread.start();                 }             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }         }      }      soul shape HttpResponseThread extends Thread {          Socket socket;         String h1;          HttpResponseThread(Socket socket, String msg){             this.socket = socket;             h1 = msg;         }          @Override         world void run() {             BufferedReader is;             PrintWriter os;             String request;              drive {                 is = novel BufferedReader(new InputStreamReader(socket.getInputStream()));                 asking = is.readLine();                  bone = novel PrintWriter(socket.getOutputStream(), true);                  String answer =                         "<html><head></head>" +                                 "<body>" +                                 "<h1>" + h1 + "</h1>" +                                 "</body></html>";                  os.print("HTTP/1.0 200" + "\r\n");                 os.print("Content type: text/html" + "\r\n");                 os.print("Content length: " + response.length() + "\r\n");                 os.print("\r\n");                 os.print(response + "\r\n");                 os.flush();                 socket.close();                                  msgLog += "Request of " + asking                         + " from " + socket.getInetAddress().toString() + "\n";                 MainActivity.this.runOnUiThread(new Runnable() {                      @Override                     world void run() {                          infoMsg.setText(msgLog);                     }                 });              } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }              return;         }     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidhttpserver.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <EditText         android:id="@+id/welcomemsg"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Welcome from " />      <TextView         android:id="@+id/infoip"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />      <ScrollView         android:layout_width="match_parent"         android:layout_height="match_parent" >          <TextView             android:id="@+id/msg"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />     </ScrollView> </LinearLayout>  

Permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml.

Implement uncomplicated HTTP server running on Android Simple HTTP server running on AndroidDownload the files (Android Studio Format) .