Search code examples
androidproxyandroid-wififile-transfernanohttpd

How to create an http server on android that a desktop can access (vice-versa) using NanoHTTPD


Using an available WiFi IP (i.e. 192.xxx.x.x, using post 8080), I'd like to create a http server in android that can connect and can be accessed by a desktop so that I can transfer files to the android device.

Update: I'm currently trying out NanoHTTPD but I can't seem to make the sample HelloWorld run on android device..

I changed the hello world code and made it extend activity instead and created an object nanohttpd.

I modified up to specifying what port to be used but upon further debugging, I found out that the server socket has no getInetAddress at all. I thought NanoHTTPD no longer needs further config...?


Solution

  • Heh, as it turns out, NanoHTTPD does the trick but I made some adjustments...

    1. Check if the device is connected.

      I used samples from manage wifi and wifi network management to create a WiFi Manager that detects all available WiFi, returns list of ScanResults, connected Scan Result and fetched the IP address.

    2. Made the NanoHttpd as a class with singleton instance.

      From the resulting connected ScanResult of WiFi Manager, I got the IP Address and passed it and a port number to the nanohttpd instance.

      NanoHttpd nanoInstance = new Nanohttpd(); String[] args = null; args[0] = the ip address; args1 = 8080; // or 8081.. etc nanoInstance.main(args);

    3. After that I finally reached the response page. =]