Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Using NanoHTTPD in Android
Add your index.html file to res/raw
Add dep to app gradle file:
dependencies {
implementation 'org.nanohttpd:nanohttpd-webserver:2.3.1'
Java class file:
import fi.iki.elonen.NanoHTTPD;
public class BasicServer extends NanoHTTPD {
private Context mCtx;
public BasicServer(Context ctx) throws IOException {
mCtx = ctx;
public Response serve(IHTTPSession session) {
String uri = session.getUri();
if (uri.equalsIgnoreCase("/")) {
return snapshot(session);
return newFixedLengthResponse(Response.Status.NOT_FOUND,MIME_PLAINTEXT,"Not Found");
private Response indexFile(IHTTPSession session) {
InputStream x = mCtx.getResources().openRawResource(R.raw.index_html);
return newFixedLengthResponse(Response.Status.OK,"text/html", x, 2038);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment