Does AJAX requests work on Webview?

Asked

Viewed 253 times

4

I got the following WebView:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.loadUrl('http://192.168.0.4:8080/map/index.html?value_id=666');

Within the index.html has a Google map, requested through the Google Maps API, which contains some requests in AJAX(GET,POST, in a local server), seeking some markers in the database. In the browser it works normally, but inside the application it does not work. My suspicion is that these requests are not working because the markers are not appearing on the map. Here came the question:

  • Does AJAX requests work in Webview? If so, what did I fail to set in Webview to work properly? If not, there is a viable alternative to circumventing it ?

See the tests

In the browser:

inserir a descrição da imagem aqui

In the app:

inserir a descrição da imagem aqui

1 answer

2

The problem you must be having is with , there is no way to disable security on webView to avoid these problems, if you have access to the API then you should add the header:

Access-Control-Allow-Origin: *

If you don’t have access to the back-end codes of the API then what you can do is create a kind of proxy in order to rewrite the headers

So I guess according to this reply from Soen can do something similar:

webView.setWebViewClient(new WebViewClient() {

    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}

I haven’t tested it yet but the answer is to actually use it shouldInterceptRequest if you don’t have access to the API’s Servers-side

Browser other questions tagged

You are not signed in. Login or sign up in order to post.