android - 如何为所有域设置 WebView 权限,如谷歌地图链接 whatsapp 聊天链接
问题描述
嘿,我是 android studio 的新手,我正在制作 webview 应用程序。我的应用程序在同一个域上完美运行,但是当我打开其他域链接时没有打开。应用程序错误截图
前任。example.com是我的主要 webview 应用程序 URL。网站在此域上完美运行,但是当我想打开其他域链接(如secondexample.com或其他域站点)时,网站无法打开。这是我的代码,请帮助我坚持这个我对此进行了研究,但没有找到解决方案。
public class MainActivity extends Activity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("https://nishaboutique.online/");
mywebView.setWebViewClient((new WebViewClient()));
// Improve performance
mywebView.getSettings().setAllowFileAccess(true);
mywebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mywebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mywebView.getSettings().setAppCacheEnabled(true);
mywebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
}
解决方案
使用此链接。它显示了类似的错误。解决方案是添加 network_security_config.xml 文件并更新清单文件。
newnetwork_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
如果这不起作用,请考虑将 url 从“http”更改为“https”。
推荐阅读
- vue.js - Vuex:在组件中使用动作与处理之间的区别?
- mysql - 如何在 laravel 中使用索引?
- ios - 如何在一个移动应用程序中部署多个不同的移动应用程序?
- javascript - 输出显示 NaN 但是,我需要数组的总和
- c# - c#类库触发构造函数间隔运行函数
- fish - 列出鱼选项卡完成中不同位置的目录
- ruby-on-rails - 我是 ruby on rails 的新手,并试图链接到“changelog_settings.html.erb”页面,但最后,它重定向到不同的页面
- html - html中的表单标签不发布/点击网站
- c++ - 如何以原子方式复制不可复制的类型
- javascript - 如何使用 Vanilla JS 在包含 div 中包装一组兄弟元素?