X
X
xotta6bl42014-12-10 17:51:20
JavaScript
xotta6bl4, 2014-12-10 17:51:20

How to make http request in JS in Android WebView?

In WebView, I load a local page from assets containing the following script.

var request = new XMLHttpRequest();
//request.open("GET", "file:///data/data/package/files/TEMP", true);
request.open("GET", "https://epub-samples.googlecode.com/files/moby-dick-20120118.epub", true);

request.responseType = 'blob';
request.onload = function () {
console.log("Q!" + request.status);
var resp = request.response;
console.log(resp.size);
};
request.send();

On the emulator 4.4.4 works. API 16/17 does not work on real devices and emulators.
On some devices
console.log(resp.size);

gives 0, on others undefined.
Webview initialization
super.getSettings().setJavaScriptEnabled(true);
        super.getSettings().setAllowContentAccess(true);
        super.getSettings().setAllowFileAccess(true);
        super.getSettings().setAllowFileAccessFromFileURLs(true);
        super.getSettings().setAllowContentAccess(true);
        super.getSettings().setAllowUniversalAccessFromFileURLs(true);
        super.setWebChromeClient(new WebChromeClient());
        super.loadUrl("file:///android_asset/html/mono.html");

Actually, why and what to do?
PS. Can you recommend epub library for android.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Yakushev, 2014-12-11
@VYakushev

Wrap your JS code in a function. Download it as before. And then the function can be called like this:
super.loadUrl("javascript:foo(var1, var2.....");

X
xotta6bl4, 2014-12-11
@xotta6bl4

As Google has shown, starting with Android 4.4 WebView uses updated and uses Chromium code and has a new JS engine. https://developer.chrome.com/multidevice/webview/o... The
old WebView doesn't seem to know XMLHttpRequest.

S
Stalker_RED, 2017-04-10
@Stalker_RED

Use 'W' instead of 'z'

W Week number of the year according to ISO-8601; weeks start on Monday For example: 42 (the 42nd week of the year)

$msg = date('W') % 2 ? 'Нечетная неделя' : 'Четная неделя';
$siteglobal['insert']['week'] = date("j.m.Y") . "<br />" . $msg;

echo $siteglobal['insert']['week'];

sandbox.onlinephpfunctions.com/code/bb9286eacdbeac...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question