首页 > 解决方案 > 如何为所有域设置 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);
    }

标签: androidandroid-studiowebwebviewmobile-website

解决方案


使用此链接。它显示了类似的错误。解决方案是添加 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”。


推荐阅读