• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

LoadURL onReceivedError() not firing

montravia

Lurker
Simple kotlin app under android studio that makes a loadURL to a local address:-

The function often fails, probably due to local net latency with:

Web Page not available
The web page at http://192.168.1.144/apikey/webcam could not be loaded because:
net: ERR_ADDRESS_UNREACHABLE

I have
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET"/>
in the manifest, and the loadurl often is fine

In order to capture the error and provide a message an
onReceivedError()
action is used.

It never fires

Is the syntax of the onReceivedError correct? It refers to WebView rather than my instance myWebview (which causes a reference error), and I've moved the scope around to no effect.

The Android Studio comment says that the function is never used. A big hint, but I can't see which scope to place it in.

I've copied this from other examples.

I'd really welcome some help please

Robin

Code:
package com.example.lilaccottagebell

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.Bundle
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel
            val name = getString(R.string.channel_name)
            val descriptionText = getString(R.string.channel_description)
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val CHANNEL_ID = "only_channel"
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = descriptionText
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(mChannel)
        }



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create channel to show notifications.
            val channelId = getString(R.string.default_notification_channel_id)
            val channelName = getString(R.string.default_notification_channel_name)
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager?.createNotificationChannel(NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH))
        }

        // If a notification message is tapped, any data accompanying the notification
        // message is available in the intent extras. In this sample the launcher
        // intent is fired when the notification is tapped, so any accompanying data would
        // be handled here. If you want a different intent fired, set the click_action
        // field of the notification message to the desired intent. The launcher intent
        // is used when no click_action is specified.
        //
        // Handle possible data accompanying notification message.
        // [START handle_data_extras]

        // [END handle_data_extras]

        val myWebView: WebView = findViewById(R.id.webview)

        /*myWebView.loadUrl("https://amazon.co.uk")*/
        myWebView.webViewClient = WebViewClient()
        WebView.setWebContentsDebuggingEnabled(true)
        /*5 March 2021*/
        myWebView.clearCache(true)






        myWebView.loadUrl("http://192.168.1.144/apikey/webcam")

        val disable_button: Button = findViewById(R.id.disable)
        disable_button.setOnClickListener {
            myWebView.loadUrl("http://192.168.1.144/apikey/disable")

        }

        fun onReceivedError(
                view: WebView,
                request: WebResourceRequest,
                error: WebResourceError
        ) {
            Toast.makeText(this, "Webcam not reachable", Toast.LENGTH_SHORT).show()
        }




    }


}




}


}
 
and neither does this:-
Code:
package com.example.lilaccottagebell

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel
            val name = getString(R.string.channel_name)
            val descriptionText = getString(R.string.channel_description)
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val CHANNEL_ID = "only_channel"
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = descriptionText
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(mChannel)
        }



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create channel to show notifications.
            val channelId = getString(R.string.default_notification_channel_id)
            val channelName = getString(R.string.default_notification_channel_name)
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager?.createNotificationChannel(NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH))
        }

        // If a notification message is tapped, any data accompanying the notification
        // message is available in the intent extras. In this sample the launcher
        // intent is fired when the notification is tapped, so any accompanying data would
        // be handled here. If you want a different intent fired, set the click_action
        // field of the notification message to the desired intent. The launcher intent
        // is used when no click_action is specified.
        //
        // Handle possible data accompanying notification message.
        // [START handle_data_extras]

        // [END handle_data_extras]

        val myWebView: WebView = findViewById(R.id.webview)

        /*myWebView.loadUrl("https://amazon.co.uk")*/
        myWebView.webViewClient = WebViewClient()
        myWebView.setWebViewClient(object : WebViewClient() {
            fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String, getContext: Context) {
                Log.i("WEB_VIEW_TEST", "error code:$errorCode")
                Toast.makeText(getContext, "Webcam not reachable",Toast.LENGTH_SHORT ).show()

            }
        })
        WebView.setWebContentsDebuggingEnabled(true)
        /*5 March 2021*/
        myWebView.clearCache(true)






        myWebView.loadUrl("http://192.168.1.144/apikey/webcam")

        val disable_button: Button = findViewById(R.id.disable)
        disable_button.setOnClickListener {
            myWebView.loadUrl("http://192.168.1.144/apikey/disable")

        }

        fun onReceivedError(
                view: WebView,
                request: WebResourceRequest,
                error: WebResourceError
        ) {
            Toast.makeText(this, "Webcam not reachable", Toast.LENGTH_SHORT).show()
        }




    }


}
 
Back
Top Bottom