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?