Question

I am attempting to use a web view with an embedded Nanohttpd server. I can get both to work , but not with each other.

I can send the WebWiew requests to other proxies that are not embedded , this works. or i can send requests from firefox to the embedded proxy and return a modified response.

But i can't send webview requests to the embedded proxy, has anyone tried or encountered this before?

The only hypothesis i can come up with for the reason it fails is i may need to run the server in the background or on another thread?

I'm using code from here for setting the webview proxy, which i can confirm does work WebView android proxy

Was it helpful?

Solution

Take a look at here You can use AsyncTask<?,?,?> to run your embedded server in the background of the UI thread. I assume your embedded server called "HelloServer", here is a example code:

public class ToRunServer extends AsyncTask<Void, Void, Boolean> {

@Override
protected Boolean doInBackground(Void... params) {
    HelloServer helloServer = null;
    try {
        helloServer = new HelloServer();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        HelloServer.start();
        System.out.println("SERVER START TRIED");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("SERVER START FAILED");
        return false;
    }
}

protected void onPostExecute(Boolean result) {
}
}

Then in your UI class or activity class just add a line into your code:

Boolean isServerRunning = new ToRunServer().execute().get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top