简体   繁体   English

Android Webview,为什么会调用onReceivedError?

[英]Android Webview, Why is onReceivedError called?

First, the code I tested is首先,我测试的代码是

class MainActivity : AppCompatActivity() {

companion object {
    private const val LOG_TAG = "WebView"
}

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

    webview.loadUrl("https://abc.xyz/investor/")

    webview.webViewClient = MyClient()
    webview.webChromeClient = WebChromeClient()
    webview.setDownloadListener { url, userAgent, contentDisposition, mimeType, contentLength ->
        Log.d(LOG_TAG, "onDownloadStart $url")
    }

}

class MyClient : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        Log.d(LOG_TAG, "shouldOverrideUrlLoading $url")
        view?.loadUrl(url)
        return true
    }

    @TargetApi(Build.VERSION_CODES.N)
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        Log.d(LOG_TAG, "shouldOverrideUrlLoading WRR")
        return shouldOverrideUrlLoading(view, request?.url.toString())
    }

    override fun onReceivedError(view: WebView?, errorCode: Int, description: String?, failingUrl: String?) {
        Log.d(LOG_TAG, "onReceivedError $errorCode $failingUrl")
        super.onReceivedError(view, errorCode, description, failingUrl)
    }

    override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
        Log.d(LOG_TAG, "onReceivedError WRR")
        super.onReceivedError(view, request, error)
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        Log.d(LOG_TAG, "onPageFinished $url")
        super.onPageFinished(view, url)
    }

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        Log.d(LOG_TAG, "onPageStarted $url")
        super.onPageStarted(view, url, favicon)
    }

    override fun onLoadResource(view: WebView?, url: String?) {
        Log.d(LOG_TAG, "onLoadResource $url")
        super.onLoadResource(view, url)
    }
}

} }

Simply load the alphabet company homepage, and then try to download the PDF file.只需加载Alphabet公司主页,然后尝试下载PDF文件。

Then you will see log like below然后你会看到如下日志

  1. shouldOverrideUrlLoading shouldOverrideUrlLoading
  2. shouldInterceptRequest应该拦截请求
  3. onLoadResource加载资源
  4. onDownloadStart下载开始
  5. onPageStarted onPageStarted
  6. onReceivedError onReceivedError
  7. onPageFinished onPageFinished

The site is healthy and the download URL is fine.该站点运行良好,下载 URL 很好。

But, why is onReceivedError call?但是,为什么要调用 onReceivedError 呢?

The error code is -1 ERROR_UNKNOWN And the tested version of Android Chrome is 78.0.3904.96.错误代码是-1 ERROR_UNKNOWN 并且Android Chrome的测试版本是78.0.3904.96。

this error is Report web resource loading error to the host application.此错误是向主机应用程序报告 web 资源加载错误。 These errors usually indicate inability to connect to the server.这些错误通常表示无法连接到服务器。 Note that unlike the deprecated version of the callback, the new version will be called for any resource (iframe, image, etc.), not just for the main page.请注意,与不推荐使用的回调版本不同,新版本将针对任何资源(iframe、图像等)调用,而不仅仅是主页。 Thus, it is recommended to perform the minimum required work in this callback.因此,建议在此回调中执行所需的最少工作。

you should go through developer documentation:您应该通过开发人员文档 go :

https://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedError(android.webkit.WebView,%20android.webkit.WebResourceRequest,%20android.webkit.WebResourceError) https://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedError(android.webkit.WebView,%)20androidkit.WebResourcewebError(android.webkit.WebView,%)20android.webResourcewebError

u have to implemented permission in Application Manifest你必须在应用程序清单中实施许可

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

also,you should check permission inactivity另外,您应该检查权限不活动

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) 
    {
     Log.v(TAG,"Permission is granted");
     //File write logic here
     return true;
    }`

If not, you need to ask the user to grant your app a permission:如果没有,您需要要求用户授予您的应用权限:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM