2

I build a webserver on my android device by NanoHttpd or ServerSocket and connect to this server from itself. It run good when device is online. But I can't receive request when device isn't online. I was try connect to localhost or 127.0.0.1 but nothing happen. Is there anyway to establish connection to localhost when device is offline?

Thanks

Luc Le
  • 196
  • 9
  • `and connect to this server from itself` ??? What do you mean by that? – greenapps Feb 06 '18 at 20:28
  • sorry my english is not good. It's mean this http server run on my device and I send request from this device. – Luc Le Feb 07 '18 at 04:40
  • And who is sending the request? And how? And what do you use? NanoHttpd or a ServerSocket? – greenapps Feb 07 '18 at 07:51
  • Scenario is show offline map tile on Mapbox. I add a RasterLayer on Mapbox and It will send request to a server for Map Tile, and I build a NanoHttpd server as a Tile Server and return Map Tile as image when Mapbox send request to it. But when tun-off wifi, the request isn't sent to NanoHttpd server – Luc Le Feb 07 '18 at 09:53
  • If the request is not send to NanoHttpd server then you cannot blame NanoHttpd server. You should blame the one who is not sending the request. So blame Mapbox if it is indead not requesting if wifi is off. – greenapps Feb 07 '18 at 09:57
  • I use NanoHttpd a lot and wifi does not have to be switched on. And i did nothing to obtain this behaviour. – greenapps Feb 07 '18 at 09:59
  • I've try send request by manual but NanoHttpd server doesn't receive this request. – Luc Le Feb 07 '18 at 10:00
  • `I've try send request by manual` ??? What would that be? – greenapps Feb 07 '18 at 10:00
  • I hard code to send request – Luc Le Feb 07 '18 at 10:01
  • Hard code? Then please show your code! And why dont you try with a browser on your device? – greenapps Feb 07 '18 at 10:02
  • Humm, It doesn't receive request from browser. Thanks – Luc Le Feb 07 '18 at 10:20
  • `It run good when device is online.` It is pretty unclear to me what you mean with online. Anyhow: wifi does not have to be switched on. – greenapps Feb 07 '18 at 10:26

1 Answers1

1

Try constructing your server using NanoHTTPD(String host, int port) constructor and specify either 127.0.0.1 (for localhost loopback interface) or 0.0.0.0 (for all interfaces). This way you will be able to connect to your server regardless of device's connectivity.

public class MyHttpd extends NanoHTTPD {
  public MyHttpd(){
    super('0.0.0.0', 8080);
    // ....
  }

  // ....
}
riyaz-ali
  • 8,677
  • 2
  • 22
  • 35
  • It doesn't work. I try `localhost`, `127.0.0.1` and `0.0.0.0` but method [serve()][1] wan't called [1]: https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java#L549 – Luc Le Feb 06 '18 at 06:54
  • can you please add the HTTP server snippet in the OP? – riyaz-ali Feb 06 '18 at 07:12