android的webview不能加载alipays:// 的scheme,提示net::ERRUNKNOWN_URL SCHEME?

新手上路,请多包涵

按照网上说的重写shouldOverrideUrlLoading也试了,还是会在onReceivedError的回调里提示errorCode为-10

阅读 3.1k
1 个回答

在安卓的WebView中加载自定义URL scheme(如alipays://)时,可能会出现 net::ERR_UNKNOWN_URL_SCHEME 错误。这是因为WebView默认不支持自定义URL scheme。要解决这个问题,需要重写 WebViewClient 的 shouldOverrideUrlLoading 方法,并在其中处理自定义的URL scheme。

前提实现 shouldOverrideUrlLoading 方法的情况下。以下是一个我写的示例你可以参考一下:


webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("alipays://")) {
            // 处理自定义URL scheme
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // 没有找到可以处理该URL的应用
                e.printStackTrace();
            }
            return true; // 表示已处理该URL,不需要WebView继续加载
        }

        // 对于其他URL,使用WebView默认行为
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        // 处理错误
    }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题