Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Simple web server using NanoHTTPD (http://elonen.iki.fi/code/nanohttpd)
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
package com.komamitsu;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import android.app.Activity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class AndroidWebServerActivity extends Activity {
private static final int PORT = 8765;
private TextView hello;
private MyHTTPD server;
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hello = (TextView) findViewById(R.id.hello);
}
@Override
protected void onResume() {
super.onResume();
TextView textIpaddr = (TextView) findViewById(R.id.ipaddr);
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
textIpaddr.setText("Please access! http://" + formatedIpAddress + ":" + PORT);
try {
server = new MyHTTPD();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
if (server != null)
server.stop();
}
private class MyHTTPD extends NanoHTTPD {
public MyHTTPD() throws IOException {
super(PORT, null);
}
@Override
public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
final StringBuilder buf = new StringBuilder();
for (Entry<Object, Object> kv : header.entrySet())
buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
handler.post(new Runnable() {
@Override
public void run() {
hello.setText(buf);
}
});
final String html = "<html><head><head><body><h1>Hello, World</h1></body></html>";
return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/ipaddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="40dp"
/>
<TextView
android:id="@+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/ipaddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="40dp"
/>
<TextView
android:id="@+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
@ArchanaGit

This comment has been minimized.

Copy link

commented Oct 3, 2012

Hi, When I tried using this code, I got the IP address as 10.0.2.15. The emulator says, WebPage not available. Can you please help me in this regard?

@ArchanaGit

This comment has been minimized.

Copy link

commented Oct 3, 2012

I used the code snippet :

public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("Exception", ex.toString());
}
return null;
}
As I was getting the IP address as 0.

@vincz777

This comment has been minimized.

Copy link

commented Dec 8, 2013

It seems that's a server.Start() is missing. I put that line after creating the MyHTTPD server variable.

@finleyChen

This comment has been minimized.

@akoster

This comment has been minimized.

Copy link

commented Dec 2, 2014

Thanks for this example! The IP reported was 0.0.0.0. I had to use 127.0.0.1 to reach the server.

@aswinpj

This comment has been minimized.

Copy link

commented Feb 14, 2016

I wish to make an app that can control all the phones features using a web server. For example I wish to turn on the flash light by clicking a button on the webpage server by nanohttpd. How can this be accompilished?

@sakkeerhussain

This comment has been minimized.

Copy link

commented Jul 5, 2016

@aswinpj Could you able to make that? In my case server is killed by Android OS after a while.

@ysfAskri

This comment has been minimized.

Copy link

commented Nov 25, 2016

how can i upload file with nanoHttp? in android ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.