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?

10
  • It looks like it tries to load localhost:8080/android_asset/ws.html?data and fails. You can use the error callbacks of WebViewClient to check why. Commented Sep 17, 2021 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. Commented Sep 21, 2021 at 5: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? Commented Sep 21, 2021 at 5: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. Commented Sep 22, 2021 at 4:31
  • I tried with another URL (not localhost), and in that case the rewriting of the URL works Commented Sep 22, 2021 at 4:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.