UPDATE 3 After testing the endpoint , I found that the request returns successfully. This indicates that there might be an issue with my Haskell Warp+WAI application server.
Could anyone suggest why a server API call would work for most browsers but fail in Android WebView?
To clarify, I want to emphasize that I am not making cross-domain requests. Despite numerous questions regarding that issue, my AJAX request is directed to the same domain where the WebView is loaded.
UPDATE 2 Instead of using jQuery, I opted for the more basic method using xhr:
function pingServer()
{
var xhr = new XMLHttpRequest();
xhr.open('get', URL + 'ping');
// Track the state changes of the request.
xhr.onreadystatechange = function()
{
var DONE = 4; // readyState 4 indicates that the request is complete.
var OK = 200; // status 200 represents a successful return.
if (xhr.readyState === DONE)
{
if (xhr.status === OK)
{
$('#submitDialog p').text(xhr.responseText + ' : ' + xhr.status);
}
else
{
$('#submitDialog p').text(xhr.responseText + ' : ' + xhr.status);
}
}
};
xhr.send(null);
}
However, this method was unsuccessful. The xhr.responseText is empty and the xhr.status is 0.
I suspect that something may be wrong with my server setup. I'm utilizing a unique server - the Haskell-based WARP with WAI. But, if that's the case, then why does everything work smoothly in other browsers?
UPDATE Upon debugging, I encountered the following error with the AJAX call shown below (a simple query to check server connectivity):
Error: NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load ''
It's worth mentioning that the actual URL I'm using loads without issues in any mobile or desktop browser, except for the Android WebView!
I have developed a basic Android application consisting of a WebView that loads a remote URL (not a local resource). Everything works fine, except that all my AJAX calls trigger the error callback immediately. Testing has been conducted on a Samsung Galaxy S6 device, targeting SDK version 21.
The failed AJAX call looks like this, with others sharing similar properties:
$.ajax(
{
url: URL + 'ping',
cache: false,
async: false,
timeout: 5000,
error: fail,
success: transmitScore
});
Both transmitScore() and fail() functions are defined elsewhere within the scope.
This is the configuration of my WebView:
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Locale;
public class Example extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView v, String s)
{
v.loadUrl("javascript: setLang('" +
Locale.getDefault().getLanguage() + "')");
}
});
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://example_url_for_privacy:2753/application");
}
}
If anyone has insights into what may be causing these issues, please share. The AJAX calls function properly in desktop Chrome and Android Chrome.
I've noticed many queries related to AJAX calls to local resource files requiring cross-domain permissions, but that does not apply in my scenario. All resources reside on the remote server, and all AJAX calls point towards it.
I also attempted setting my WebView to use WebChromeClient without observing any changes. It seems that nowadays, this is the default behavior for WebViews. In the AJAX calls, I experimented with various combinations of async and cache, yet none proved successful.