我正在尝试使用Web View制作应用程序,我需要在资产文件中发送来自JavaScript的Ajax请求。 (对不起,我的英语)
我编写了用于从资产加载文件的代码,但是当我发出Ajax请求时,它总是给出readyState:0 status:0
MainActivity.java
String TAG = "MainActivityTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, url);
if(url.contains("file:///")){
view.loadUrl(url);
return false;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
CookieManager.getInstance().setAcceptCookie(true);
String url = "file:///android_asset/index.html";
webView.loadUrl(url);
}
assets / index.html
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Hello</h1>
<span id="text"></span>
</body>
</html>
<script src="jquery-3.3.1.js"></script>
<script>
$.get("http://api.samosval/user/get", function(data) {
$("#text").text(JSON.stringify(data));
}).fail(function(data) {
$("#text").text(JSON.stringify(data));
});
</script>
加载应用程序时出现错误 https://pp.userapi.com/c850528/v850528787/929d9/2GLghO0p1gI.jpg
这是日志
1-15 02:00:54.068 13863-13863/com.arystankaliakparov.test15ver D/OpenGLRenderer: Skia GL Pipeline
01-15 02:00:54.120 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unsupported mime video/divx
01-15 02:00:54.121 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unsupported mime video/divx311
01-15 02:00:54.124 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unsupported mime video/divx4
01-15 02:00:54.134 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unrecognized profile 4 for video/hevc
01-15 02:00:54.136 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2
01-15 02:00:54.138 13863-13922/com.arystankaliakparov.test15ver W/VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2
01-15 02:00:54.141 13863-13924/com.arystankaliakparov.test15ver I/OpenGLRenderer: Initialized EGL, version 1.4
01-15 02:00:54.141 13863-13924/com.arystankaliakparov.test15ver D/OpenGLRenderer: Swap behavior 2
01-15 02:00:54.174 13863-13922/com.arystankaliakparov.test15ver I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
01-15 02:00:54.177 13863-13922/com.arystankaliakparov.test15ver W/Utils: could not parse long range '175-174'
01-15 02:00:54.192 13863-13931/com.arystankaliakparov.test15ver W/cr_CrashFileManager: /data/user/0/com.arystankaliakparov.test15ver/cache/WebView/Crash Reports does not exist or is not a directory
01-15 02:00:54.451 13863-13907/com.arystankaliakparov.test15ver D/NetworkSecurityConfig: No Network Security Config specified, using platform default
答案 0 :(得分:0)
我找到了API 23级别更高的设备的答案。您应该在清单中启用usesCleartextTraffic。像这样:
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true">
...
</application>
</manifest>