【问题标题】:Webview not loading url in androidWebview没有在android中加载url
【发布时间】:2021-08-26 18:27:50
【问题描述】:

我有这个代码。

@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebPageScreen(urlToRender: String) {
  AndroidView(factory = {
    WebView(it).apply {
      layoutParams = ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.MATCH_PARENT
      )
      webViewClient = WebViewClient()
      loadUrl("https:....") //Url to be loaded
    }
  }, update = {
    it.loadUrl("https:...") //Url to be loaded
  })
}

我正在尝试加载这个url

当我加载此页面时,它正在重定向但显示错误是什么问题以及如何解决这个问题?

【问题讨论】:

  • 您的代码与google.com 完全兼容。使用您的网址,我在控制台中收到此错误:E/chromium: [ERROR:aw_contents_client_bridge.cc(257)] No client certificate selected。所以问题与jetpack compose无关,我建议您将标签和标题更改为WebView not loading url on android
  • 现在我尝试使用 http 而不是 https 它显示错误。我附上了错误的屏幕截图。如何解决此问题
  • 你试过在stackoverflow上查看这个错误代码吗?
  • 是的,我尝试了大多数可能的解决方案

标签: android android-webview


【解决方案1】:

创建这个文件

res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">URL</domain>
    </domain-config>
</network-security-config>

在应用程序内部的清单中添加这一行

android:networkSecurityConfig="@xml/network_security_config"

或

你可以在清单中使用这一行

android:usesCleartextTraffic="true"

如果您的网站尚未使用 SSL,您可以使用certbot 免费 SSL

【讨论】:

  • 我现在试过了,但仍然显示同样的错误
  • 您是否已经将网址更改为您的网址?
  • 或者您可以尝试将 networkSecurityConfig 更改为 android:usesCleartextTraffic="true"
  • 是的,我试过了,我认为网站所有其他网站都存在问题,仅加载此网站未加载。
  • 是的,我将 URL 替换为我的 URL
【解决方案2】:

webview 不适用于特定的 url,因为该网站的授权者已经以这种方式保护了它。要在 webview 中访问该 url,我们需要从权威机构获取客户端证书。

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2016-07-18
    相关资源
    最近更新 更多