我正在开发一个android应用程序。 我的应用程序中有一个Webview,它将加载一个网页。
webView = findViewById(R.id.web_view);
webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 SECSSOBrowserChrome");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("xxxx.html");
我用https://www.google.com
测试过,webView正常工作。
但是它无法加载xxx.html(此站点受Cloudflare保护)
我这样添加了WebViewClient
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
KLog.d(TAG, "onPageStarted " + url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
KLog.d(TAG, "host =" + request.getUrl().toString());
view.loadUrl(request.getUrl().toString());
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
KLog.d(TAG, "onPageFinished " + url);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
KLog.d(TAG,"onReceivedError " + error.getDescription() + "Error code = " + error.getErrorCode());
}
});
但是public void onPageStarted(WebView视图,字符串url,位图图标){永不调用。
等待很长时间后,我得到了这张图片
https://drive.google.com/file/d/12DmOGWNqKcq5IsMiQEApmtnvequOIxqD/view?usp=drivesdk
您能帮我使用android WebView加载此页面吗?
预先感谢
答案 0 :(得分:0)
在Network security configuration
从Android 9(API级别28)开始,已禁用明文支持 默认情况下。
要在应用中启用基于 HTTP 的URL(不推荐),请在AndroidManifest.xml
标签的application
文件中添加以下代码
android:usesCleartextTraffic="true"
答案 1 :(得分:0)
这是因为您尝试访问的服务器不安全,即它使用的是HTTP(不是HTTPS)。
Android P默认使用HTTPS。这意味着,如果您在应用程序中使用未加密的HTTP请求,则该应用程序将在除Android P之外的所有Android版本中正常运行。
为避免这种安全性,请尝试对应用代码进行以下更改。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>
,然后在res / xml中添加名为network_security_config.xml
在network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
// Add host of your URL in below line.
// ie. if url is "https://www.google.com/search?source=...."
// then just add "www.google.com"
<domain includeSubdomains="true">www.myanmartvchannel.com</domain>
</domain-config>
</network-security-config>