0

I have a webview that is displaying a specific page from a website (an OAuth authentication page). When the user validates the page, s/he will be redirected to a new page with an URL like https://localhost:8080/.... I would like to detect that and change it to a file:///... URL. I tried to modify the onPageFinished function (see below), but it doesn't work (NB: my code sends a message by Bluetooth with writeMessage to get the URL of the OAuth service back in onReadData) :

class ConfigWeatherStationFragment : Fragment(), BleManagerListener {

    private val webViewClient = object : WebViewClient() {

        override fun onPageFinished(view: WebView, url: String?) {
            super.onPageFinished(view, url)
            binding.configWsPBar.visibility = ProgressBar.GONE
            if (url != null) {
                if(url.startsWith("https://localhost:8080/")) {
                    Log.i("WS", "replaced")
                    binding.configWsWeb.loadUrl(url.replace("https://localhost:8080/", "file:///"))
                }
                Log.i("Loaded", url)
            }
        }
    }

    // onCreate, onCreateView... getting binding...

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        bleManager.writeMessage(BleManager.FUNCTION_WEATHER_STATION, BleManager.WEATHER_STATION_REQUEST)
    }

    override fun onReadData(data: String) { // BleManagerListener
        Log.i("WS", data)
        if(URLUtil.isValidUrl(data))
        {
            askAuthorisation(data)
        }
        else
        {
            TODO("Process the acknowledgement")
        }
    }

    private fun askAuthorisation(url: String)
    {
        var webView = binding.configWsWeb
        webView.post(Runnable {
            webView.settings.allowContentAccess = true
            webView.settings.allowFileAccess = true
            webView.webViewClient = webViewClient
            webView.loadUrl(url)
        })
    }

}

Even if on the Logcat I can see the Replace message, the web page keeps the same, telling me that :

Web page not available

The web page at https://localhost:8080/android_asset/ws.html?data could not be loaded because:

net::ERR_CONNECTION_REFUSED

I tried to put the reload part in a webView.post(Runnable { ... }) again, but the result is the same. I also tried with another URL, like https://www.google.com with the same result.

How can I reload my webpage to look for my file?

Dark Patate
  • 113
  • 2
  • 12
  • It looks like it tries to load https://localhost:8080/android_asset/ws.html?data and fails. You can use the error callbacks of WebViewClient to check why. – gioravered Sep 17 '21 at 12:05
  • I caught the error callback and I got `-6: net::ERR_CONNECTION_REFUSED` same as the browser. I also tried to reload the URL inside the error callback, but it doesn't work. Maybe I cannot do the reload like that? It looks like, on the Logcat, I never made it. – Dark Patate Sep 21 '21 at 05:34
  • In order for localhost:8080/android_asset/ws.html?data to work, you need a server running on your localhost, using post 8080. Why aren't you loading ws.html as a static file from your assets folder? Why using localhost? – gioravered Sep 21 '21 at 05:47
  • @gioravered it's because, for the redirect URL of the OAuth service, I cannot use `file:///` URLs for security reasons. That's why I'm using the localhost one, with the aim to rewrite any localhost URL to the file one. – Dark Patate Sep 22 '21 at 04:31
  • I tried with another URL (not localhost), and in that case the rewriting of the URL works – Dark Patate Sep 22 '21 at 04:48
  • I've tested your code and used "https://www.google.com" as the initial url, and inside the onPageFinished replaced it with "https://www.facebook.com" and it worked. – gioravered Sep 22 '21 at 05:34
  • Yeah, if the loading URL is not localhost, it works also for me. So I don't know if the problem is because of localhost or if because the page fails to load. Anyway, I think I can use another URL to rewrite, it'll be the simpler solution, I guess. – Dark Patate Sep 22 '21 at 06:03
  • You can override onReceivedError of WebViewClient and check the url there? – gioravered Sep 22 '21 at 06:35
  • I tried to rewrite the URL there, but it didn't work also – Dark Patate Sep 22 '21 at 07:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237347/discussion-between-gioravered-and-dark-patate). – gioravered Sep 22 '21 at 07:48

0 Answers0