【问题标题】:use appassets.androidplatform.net without ssl (for sending ajax requsets by http)使用不带 ssl 的 appassets.androidplatform.net(用于通过 http 发送 ajax 请求)
【发布时间】:2020-09-26 17:52:34
【问题描述】:

我正在开发 webview android 应用程序,我通过“https://appassets.androidplatform.net/assets/www/index.html”加载我的 html 和 js 代码,但在我的 js 代码中我使用 ajax 请求到在没有 ssl 的情况下运行的 API,所以 js 不允许我从 https 源向 http 服务器发出 ajax 请求。当我尝试使用http://appassets.androidplatform.net/assets/www/index.html 时,我收到错误“net::ERR_NAME_NOT_RESOLVED”。有没有什么方法可以让它在不为 API 服务器购买 ssl 或使用 file:// 协议加载代码的情况下工作?

我不知道它是否有帮助,但这是我的 WebViewAssetLoader 代码:

assetLoader = new WebViewAssetLoader.Builder()
            .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
            .addPathHandler("/res/", new WebViewAssetLoader.ResourcesPathHandler(this))
            .build();

public WebResourceResponse shouldInterceptRequest(WebView view,
                                                      WebResourceRequest request) {
        if (String.valueOf(request.getUrl()).endsWith(".js")) {
            try {
                return new WebResourceResponse("text/javascript", "UTF-8", getAssets().open(String.valueOf(request.getUrl()).replace("http://appassets.androidplatform.net/assets/","")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return assetLoader.shouldInterceptRequest(request.getUrl());
    }

【问题讨论】:

    标签: java android-webview


    【解决方案1】:

    答案很简单 首先,我必须将 android:usesCleartextTraffic="true" 添加到标签中。 其次,我必须使用 webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);在我的活动中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2018-05-14
      • 2014-11-24
      • 2017-08-12
      • 1970-01-01
      • 2013-01-15
      • 2018-06-21
      相关资源
      最近更新 更多