Issue
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());
}
Solution
The answer was quite simple Firslty, I have to add android:usesCleartextTraffic="true" into tag. And secondly I have to use webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); in my activity.
Answered By - D. Stevenson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.