尽管站点是 HTTPS,但 WebView 显示 ERR_CLEARTEXT_NOT_PERMITTED [英] WebView showing ERR_CLEARTEXT_NOT_PERMITTED although site is HTTPS

查看:32
本文介绍了尽管站点是 HTTPS,但 WebView 显示 ERR_CLEARTEXT_NOT_PERMITTED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 Android 上开发一个应用程序,所以我没有太多东西.到目前为止,我所拥有的只是一个 WebView.我在 Android Studio 中创建了该项目,并将我的项目设置为 Android InstantApp.我不确定为什么/如何,但我的猜测是我在创建项目时忽略了它的一个选项.

I'm starting to work on an app on Android, so I don't have much. All I have is just a WebView so far. I created the project in Android Studio, and my project got set as an Android InstantApp. I'm not sure why/how, but my guess is that I overlooked an option for it when creating the project.

我从 WebView 收到一个错误,提示 net::ERR_CLEARTEXT_NOT_PERMITTED.当我搜索错误时,我看到当应用程序是 InstantApp 时,WebViews 只能加载 HTTPS 的站点,而不能加载 HTTP 站点.

I was getting an error from the WebView saying net::ERR_CLEARTEXT_NOT_PERMITTED. When I googled the error, I saw that when an app is an InstantApp, WebViews can only load sites that are HTTPS, and cannot load an HTTP site.

此应用程序的目的是成为仅用于一个站点的极其简单的 Flash 播放器.这是为了在运行需要 Flash 的游戏时有更好的性能.这个游戏在darkorbit.com,这是HTTPS.

The purpose of this app is to be an extremely simple Flash player for only one site. This is to have better performance running a game that requires Flash. This game is at darkorbit.com, which is HTTPS.

MainActivity.java:

MainActivity.java:

package com.tylerr147.darkorbit;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = findViewById(R.id.webView1);
        wv.loadUrl("https://darkorbit.com/");
        wv.setWebViewClient(new CustomWebViewClient());
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);

    }
}

和 CustomWebViewClient.java

and CustomWebViewClient.java

package com.tylerr147.darkorbit;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

我的问题:如何将我的应用程序禁用为 InstantApp,或者如何让此 WebView 显示该站点?

My question: How can I disable my app as an InstantApp, or how can I get this WebView to display the site?

我觉得我也提到一些其他细节很重要:在显示 WebView 的应用程序中,它还显示位于 http://darkorbit.com/ 的网页" 无法加载,因为:net::ERR_CLEARTEXT_NOT_PERMITTED

I feel like it's important I mention a few other details too: In the app, where it is showing the WebView, it also says "The webpage at http://darkorbit.com/" could not be loaded because: net::ERR_CLEARTEXT_NOT_PERMITTED

请注意,...位于 http://darkorbit.com/ 的站点...",而不是... site at https://darkorbit.com/ ..."即使URL 是硬编码的,上面写着https://darkorbit.com/".此外,我正在一个模拟器上测试该应用程序,该模拟器设置为运行 Android 9 的 Google Pixel 2.

Notice that is says "... site at http://darkorbit.com/ ...", and not "... site at https://darkorbit.com/ ..." even though the string for the URL is hardcoded, and says "https://darkorbit.com/". Also, I am testing the app on an emulator set up as a Google Pixel 2 running Android 9.

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thank you.

推荐答案

解决方案:

在您的 application 标签中添加以下行:

Add the below line in your application tag:

android:usesCleartextTraffic="true"

如下图:

<application
    ....
    android:usesCleartextTraffic="true"
    ....>

更新:如果您有网络安全配置,例如:android:networkSecurityConfig="@xml/network_security_config"

UPDATE: If you have network security config such as: android:networkSecurityConfig="@xml/network_security_config"

无需如上所示将明文流量设置为 true,而是使用以下代码:

No Need to set clear text traffic to true as shown above, instead use the below code:

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

    <base-config cleartextTrafficPermitted="false"/>
</network-security-config>  

cleartextTrafficPermitted 设置为 true

希望有帮助.

这篇关于尽管站点是 HTTPS,但 WebView 显示 ERR_CLEARTEXT_NOT_PERMITTED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆