1. Are you ready for the Galaxy S20? Here is everything we know so far!

LoadURL onReceivedError() not firing

Discussion in 'Android Development' started by montravia, Mar 10, 2021.

  1. montravia

    montravia Lurker
    Thread Starter

    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 (Text):
    1.  
    2. package com.example.lilaccottagebell
    3.  
    4. import android.app.NotificationChannel
    5. import android.app.NotificationManager
    6. import android.os.Build
    7. import android.os.Bundle
    8. import android.webkit.WebResourceError
    9. import android.webkit.WebResourceRequest
    10. import android.webkit.WebView
    11. import android.webkit.WebViewClient
    12. import android.widget.Button
    13. import android.widget.Toast
    14. import androidx.appcompat.app.AppCompatActivity
    15.  
    16. class MainActivity : AppCompatActivity() {
    17.  
    18.     override fun onCreate(savedInstanceState: Bundle?) {
    19.         super.onCreate(savedInstanceState)
    20.         setContentView(R.layout.activity_main)
    21.  
    22.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    23.             // Create the NotificationChannel
    24.             val name = getString(R.string.channel_name)
    25.             val descriptionText = getString(R.string.channel_description)
    26.             val importance = NotificationManager.IMPORTANCE_DEFAULT
    27.             val CHANNEL_ID = "only_channel"
    28.             val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    29.             mChannel.description = descriptionText
    30.             // Register the channel with the system; you can't change the importance
    31.             // or other notification behaviors after this
    32.             val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    33.             notificationManager.createNotificationChannel(mChannel)
    34.         }
    35.  
    36.  
    37.  
    38.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    39.             // Create channel to show notifications.
    40.             val channelId = getString(R.string.default_notification_channel_id)
    41.             val channelName = getString(R.string.default_notification_channel_name)
    42.             val notificationManager = getSystemService(NotificationManager::class.java)
    43.             notificationManager?.createNotificationChannel(NotificationChannel(channelId,
    44.                     channelName, NotificationManager.IMPORTANCE_HIGH))
    45.         }
    46.  
    47.         // If a notification message is tapped, any data accompanying the notification
    48.         // message is available in the intent extras. In this sample the launcher
    49.         // intent is fired when the notification is tapped, so any accompanying data would
    50.         // be handled here. If you want a different intent fired, set the click_action
    51.         // field of the notification message to the desired intent. The launcher intent
    52.         // is used when no click_action is specified.
    53.         //
    54.         // Handle possible data accompanying notification message.
    55.         // [START handle_data_extras]
    56.  
    57.         // [END handle_data_extras]
    58.  
    59.         val myWebView: WebView = findViewById(R.id.webview)
    60.  
    61.         /*myWebView.loadUrl("https://amazon.co.uk")*/
    62.         myWebView.webViewClient = WebViewClient()
    63.         WebView.setWebContentsDebuggingEnabled(true)
    64.         /*5 March 2021*/
    65.         myWebView.clearCache(true)
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.         myWebView.loadUrl("http://192.168.1.144/apikey/webcam")
    73.  
    74.         val disable_button: Button = findViewById(R.id.disable)
    75.         disable_button.setOnClickListener {
    76.             myWebView.loadUrl("http://192.168.1.144/apikey/disable")
    77.  
    78.         }
    79.  
    80.         fun onReceivedError(
    81.                 view: WebView,
    82.                 request: WebResourceRequest,
    83.                 error: WebResourceError
    84.         ) {
    85.             Toast.makeText(this, "Webcam not reachable", Toast.LENGTH_SHORT).show()
    86.         }
    87.  
    88.  
    89.  
    90.  
    91.     }
    92.  
    93.  
    94. }
    95.  



    }


    }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. montravia

    montravia Lurker
    Thread Starter

    and neither does this:-
    Code (Text):
    1.  
    2. package com.example.lilaccottagebell
    3.  
    4. import android.app.NotificationChannel
    5. import android.app.NotificationManager
    6. import android.content.Context
    7. import android.os.Build
    8. import android.os.Bundle
    9. import android.util.Log
    10. import android.webkit.WebResourceError
    11. import android.webkit.WebResourceRequest
    12. import android.webkit.WebView
    13. import android.webkit.WebViewClient
    14. import android.widget.Button
    15. import android.widget.Toast
    16. import androidx.appcompat.app.AppCompatActivity
    17.  
    18. class MainActivity : AppCompatActivity() {
    19.  
    20.     override fun onCreate(savedInstanceState: Bundle?) {
    21.         super.onCreate(savedInstanceState)
    22.         setContentView(R.layout.activity_main)
    23.  
    24.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    25.             // Create the NotificationChannel
    26.             val name = getString(R.string.channel_name)
    27.             val descriptionText = getString(R.string.channel_description)
    28.             val importance = NotificationManager.IMPORTANCE_DEFAULT
    29.             val CHANNEL_ID = "only_channel"
    30.             val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    31.             mChannel.description = descriptionText
    32.             // Register the channel with the system; you can't change the importance
    33.             // or other notification behaviors after this
    34.             val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    35.             notificationManager.createNotificationChannel(mChannel)
    36.         }
    37.  
    38.  
    39.  
    40.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    41.             // Create channel to show notifications.
    42.             val channelId = getString(R.string.default_notification_channel_id)
    43.             val channelName = getString(R.string.default_notification_channel_name)
    44.             val notificationManager = getSystemService(NotificationManager::class.java)
    45.             notificationManager?.createNotificationChannel(NotificationChannel(channelId,
    46.                     channelName, NotificationManager.IMPORTANCE_HIGH))
    47.         }
    48.  
    49.         // If a notification message is tapped, any data accompanying the notification
    50.         // message is available in the intent extras. In this sample the launcher
    51.         // intent is fired when the notification is tapped, so any accompanying data would
    52.         // be handled here. If you want a different intent fired, set the click_action
    53.         // field of the notification message to the desired intent. The launcher intent
    54.         // is used when no click_action is specified.
    55.         //
    56.         // Handle possible data accompanying notification message.
    57.         // [START handle_data_extras]
    58.  
    59.         // [END handle_data_extras]
    60.  
    61.         val myWebView: WebView = findViewById(R.id.webview)
    62.  
    63.         /*myWebView.loadUrl("https://amazon.co.uk")*/
    64.         myWebView.webViewClient = WebViewClient()
    65.         myWebView.setWebViewClient(object : WebViewClient() {
    66.             fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String, getContext: Context) {
    67.                 Log.i("WEB_VIEW_TEST", "error code:$errorCode")
    68.                 Toast.makeText(getContext, "Webcam not reachable",Toast.LENGTH_SHORT ).show()
    69.  
    70.             }
    71.         })
    72.         WebView.setWebContentsDebuggingEnabled(true)
    73.         /*5 March 2021*/
    74.         myWebView.clearCache(true)
    75.  
    76.  
    77.  
    78.  
    79.  
    80.  
    81.         myWebView.loadUrl("http://192.168.1.144/apikey/webcam")
    82.  
    83.         val disable_button: Button = findViewById(R.id.disable)
    84.         disable_button.setOnClickListener {
    85.             myWebView.loadUrl("http://192.168.1.144/apikey/disable")
    86.  
    87.         }
    88.  
    89.         fun onReceivedError(
    90.                 view: WebView,
    91.                 request: WebResourceRequest,
    92.                 error: WebResourceError
    93.         ) {
    94.             Toast.makeText(this, "Webcam not reachable", Toast.LENGTH_SHORT).show()
    95.         }
    96.  
    97.  
    98.  
    99.  
    100.     }
    101.  
    102.  
    103. }
    104.  
     
Loading...

Share This Page

Loading...