Access JS code from Android Android app

This may seem like a strange problem, but I'm curious to know if this will work. I am working on POC and therefore must either prove or disprove that this works or not.

The user interface in the Android application would be native (Java + XML layouts) + some other device access functions (e.g. Camera / File System, etc.).

There is a JS library that I created that has several functions that perform an Ajax post and receive requests.

In the application, I have an invisible Webview where I load empty HTML (referring to this JS library). And in this WebView, I introduced JavascripInterface. Thus, in essence, the user interface would be native, and you have never seen Webview. This is just a host that provides access to my JS library to its own code.

Now, with some actions in my user interface, I call JS functions in Webview, which, in turn, tries to make an ajax call (loadUrl calls ex. Javascipt: functionName ()). But these challenges fail without visible errors.

Note: This same HTML file works if I download it to my desktop browser. AJAX call succeeds.

But, when I initiate Ajax calls through JavascriptInterface (or webview.loadUrl ()) calls, they fail with a response status of 0.

Everything except AJAX, such as simple function calls, warnings, and callbacks through the javascript interface, works fine.

Q: I know that this is a strange and impractical way to do something. But, will this work / ?

Update: Even after installation

  setBlockNetworkLoads (false) 
it still doesn't work.

I tried to log JS calls and errors and got this error.

The X-Requested-With request header field is not allowed by Access-Control-Allow-Headers.

Any idea how to solve this?

+4
source share
2 answers

It seems that you are trying to execute an ajax request for the cross domain.

Cross-domain requests are not allowed by the same origin policy, so requests will be blocked. If you upload a local file to webView and then send ajax requests from it to other domains, it will be so.

If this is the case, and it is the same origin policy that is causing you problems, you might want to look at a resource share (CORS) or JSONP to get around it.

Given the error that appears, your problem seems to be similar to the one discussed here: AJAX cross-domain does not send the X-Requested-With header

You might want to change the server settings to allow the X-Requested-With header.

It also seems that from the API level 16, the setAllowFileAccessFromFileURLs () method has been added to webSettings. Setting this parameter to true for webView can also solve the problem.

+1
source

I had a similar problem when I loaded the โ€œweb applicationโ€ locally into the WebView , just WebView Ajax remotely. I observed a similar problem when Javascript warnings etc. They worked fine, but AJAX calls were not made. It turned out that by default WebView blocks network loads .

Make sure you do this:

webView.getSettings().setBlockNetworkLoads(false);

It did it for me. To clarify, I did not use Javascriptinterface - just load the as-using web application using webView.loadDataWithBaseUrl() - the baseUrl parameter passed to this method was where I make all my AJAX calls (since this method refers to one and same origin policy)

0
source

Source: https://habr.com/ru/post/1494031/


All Articles