3

I'm building an android app with xamarin. In it I need to load files from a local storage through a webserver into a webview. So I used NanoHttpd to create a webserver to serve files to the webview. Iloaded the jars into a jar binding library. Started a web server the same way as in the samples. Unfortunately when I try to load http://localhost:8080/ in the webview I keep getting err_connection_refused. I'm loading the app in a Sony Xperia device.

I tried alternatives to NanoHttpd too. EmbedIO for exaple. I tried to used the included SimpleWebServer or make my own that will return even a simple string just to see that it's working. I tried to load 127.0.0.1 or localhost, tried different ports. Nothing works.

Help me StackOverflow, you're my only hope.

Edit to add some code

This is an example implementation copied from the helloserver sample

 class HttpServer : NanoHTTPD
    {
        public static void main(String[] args)
        {
            ServerRunner.Run(Class.FromType(typeof(HttpsServer)));
        }


        public HttpServer():base(8080)
        {

        }

        public HttpServer(int port) : base(port)
        {

        }


        public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session)
        {
            NanoHTTPD.Method method = session.Method;
            String uri = session.Uri;
            //HelloServer.LOG.info(method + " '" + uri + "' ");

            String msg = "<html><body><h1>Hello server</h1>\n";
            var parms = session.Parms;
            if (parms["username"] == null)
            {
                msg += "<form action='?' method='get'>\n" + "  <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
            }
            else
            {
                msg += "<p>Hello, " + parms["username"] + "!</p>";
            }

            msg += "</body></html>\n";

            return new NanoHTTPD.Response(msg);
        }
    }

Here's the initialization:

private void StartServer()
        {

                String[] args = {

                    "--dir",
                    Application.Context.FilesDir + "/Website"
                };
                SimpleWebServer.Main(args);

                //HttpsServer.main(null);
                //  server=new HttpsServer();
               // server.Start();


        }

Here it's the simplewebserver from the nanohttpd package, result connection refused and logcat shows

[INFO:CONSOLE(12)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (12)

If I try to start with Httpserver.main I get the same and additionally

java.lang.InstantiationException: class md535a550fd04ddaa12bd0435e04d30336e.HttpServer has no zero argument constructor

If I try to start with new httpserver and then server.start I get just the string "Not Found" on the webview and no logcat errors that I can see

Alex
  • 61
  • 5
  • Try that url with browsers on your device first. – greenapps Sep 16 '15 at 06:07
  • Have you request certain permissions in manifest? – Lex Li Sep 16 '15 at 06:10
  • I have permission internet and permission network state in manifest. It doesn't work in browser either. I'm also running the server on a parallel thread. – Alex Sep 16 '15 at 06:37
  • from an emulator like genymotion it works? – CDrosos Sep 16 '15 at 08:22
  • 'I'm also running the server on a parallel thread.'. A parallel thread? Why? Make it a different app to begin with. NanoHttpd is very ok. – greenapps Sep 17 '15 at 10:52
  • I tried running on an emulator, same result. Tried running on the UI thread, same result. You can see why I'm close to despairing here. I edited the question to add my code. – Alex Sep 18 '15 at 18:04
  • 2
    I solved the same issue by calling `start()` after `super(8080)` in my derived class constructor – Ivan Ferrer Villa Mar 28 '16 at 03:38

0 Answers0