4

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...?

Stephan
  • 41,764
  • 65
  • 238
  • 329
eunique0216
  • 875
  • 1
  • 16
  • 28

2 Answers2

3

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. =]

eunique0216
  • 875
  • 1
  • 16
  • 28
0

Use an embeddable http server: http://tjws.sourceforge.net/

Note: most mobile devices are behind NAT (both on Wifi and cell networks), where inbound connections are not possible. This means you can not initiate connection to them from internet.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154