32

I load a local html file (from assets folder) to the app WebView. In the HTML I run a jQuery.getJSON(url). the url is a remote server.

This action fails, and I'm guessing because of a different origin issue (cross domain). I run the same file on chrome and there it specifically says so.

Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?

oriharel
  • 10,418
  • 14
  • 48
  • 57
  • 1
    Did you try adding the "&jsoncallback=?" or "callback=?" to your URL ? – Zakaria Dec 27 '11 at 19:54
  • Did you try with the ".ajax" method instead of "getJSON" ? with ".ajax", if you put in the options "data-type:jsonp", it should work for you! – Zakaria Dec 27 '11 at 19:56
  • won't the remote server need to specifically support JSONP for that to work? I'm not super familiar with the rules of webviews, are they different from desktop browsers? – mattacular Dec 27 '11 at 20:02
  • I have a callback. in fact when running the html from the built-in web browser of Eclipse it works fine. haven't tried to simply call ".ajax" but why would it be different? I'll try anyway... – oriharel Dec 27 '11 at 20:16
  • Did you ever manage to get this working? I have hit the exact same problem and can't fathom it out (as another data point, the exact same form works okay on an embedded browser on iPhone). – Chris Rae Mar 15 '13 at 22:52

5 Answers5

60

Today morning I found solution that seems to be working.

The Java part

Initialize your WebView:

WebView _webView = (WebView) this.findViewById(R.id.id_of_your_webview_in_layout);

get WebView settings:

WebSettings settings = _webView.getSettings();

set following settings:

settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true); //Maybe you don't need this rule
settings.setAllowUniversalAccessFromFileURLs(true);

now you can load your your html file by standard way:

_webView.loadUrl("file:///android_asset/www/index.html");

The Javascript part

Create XHR request by standard way

var xhr = new XMLHttpRequest();
xhr.open("get", "http://google.com", false);
xhr.send();

Print the result somewhere

document.body.innerHTML = xhr.responseText

NOTICE: This procedure works only on API level 16 or higher (At least the documentation says that).

Stepan Vrany
  • 1,026
  • 9
  • 14
  • 2
    According to the documentation for setAllowFileAccessFromFileURLs and setAllowUniversalAccessFromFileURLs, they used to be set to true for ICS and older. Starting in JellyBean they turned it off. If you are trying to load a local JS file from a local HTML page, then you'll need to enable these. – Steven Apr 03 '13 at 22:34
  • Get the following: NETWORK_ERR: XMLHTTPRequest exception 101 – Codebeat Mar 12 '14 at 13:09
  • 1
    great ans :) perfect solution :) u saved my day.thanks a ton. – Nevaeh Oct 17 '14 at 05:53
  • Life saver, hot stuff congrats!! For me all I needed was `settings.setAllowUniversalAccessFromFileURLs(true);` – Heitor Dec 31 '17 at 08:51
  • Perfect, this is what i was needed – Sunil Aug 08 '18 at 09:05
  • You Perfect :))) – Ucdemir Dec 24 '22 at 13:49
2

Don't forget to add the internet permission in your manifest file:

<uses-permission android:name="android.permission.INTERNET"/>

Also make sure you are using JSONP requests (don't forget the &callback=? as stated above)

brz
  • 1,846
  • 21
  • 21
1

I load a local html file (from assets folder) to the app WebView

Note that you failed to say how you are doing this. I am going to guess that it was by a loadUrl() on a file:///android_asset URL.

Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?

Try using loadDataWithBaseURL() to load the content, supplying a URL on the remote server as the base URL.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

A solution we used was to use Android to get the update files from the server, place them and overwrite files in the web folder, and then browse.

Cyberience
  • 972
  • 10
  • 15
0

Ajax calls wont work from local file system. More over it will become cross-domain. So it wont work. It worked in eclipse, becuz you tried from local server.

Prem
  • 1