使用 appassets.androidplatform.net 而不使用 SSL(用于通过 HTTP 发送 AJAX 请求)

huangapple go评论154阅读模式
英文:

use appassets.androidplatform.net without ssl (for sending ajax requsets by http)

问题

我正在开发一个 WebView 安卓应用程序,我通过 "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());
        }
英文:

I`m working on webview android app, and I load my html and js code via "https://appassets.androidplatform.net/assets/www/index.html", but in my js code I use ajax requests to API which is running without ssl, so js doesnt allow me to make ajax requests from https origin to http server. When I try to use http://appassets.androidplatform.net/assets/www/index.html I get an error 'net::ERR_NAME_NOT_RESOLVED'. Is there any way to make it work without buying ssl for API server or using file:// protocol for loading code?

I dont know if it helps, but here is my code for WebViewAssetLoader:

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

and

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());
    }

答案1

得分: 0

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

英文:

The answer was quite simple
Firslty, I have to add android:usesCleartextTraffic="true" into <application> tag.
And secondly I have to use webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); in my activity.

huangapple
  • 本文由 发表于 2020年9月27日 01:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64080873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定