Android webview ajax
Solution 1
Finally I've found the answer after 3 Long days.
the problem was in the page that I request which have this code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
in the response which conflict with android webview and doesn't work with jquery selectors ... Don't know why!!!
but when I removed the above code from response the page and its ajax works fine.
P.S: all my pages are xhtml not html.
Solution 2
- Check if you have
<uses-permission android:name="android.permission.INTERNET" />
in your AppManifest.xml file. - If javascript in the loaded page makes requests to some site other than
http://192.168.1.236:8080
and that site does not allow Cross-Origin XMLHTTPRequests, then these requests will fail due to security restrictions of WebView.
Mohamed Habib
Updated on June 04, 2022Comments
-
Mohamed Habib almost 2 years
I have wep application which some pages have to do ajax requests to get and update that pages without refresh the page.
My problem when I use android WebView to load that wep application. all pages that request ajax doesn't update the page which means the ajax requests don't work.
here is the code of MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview_layout); webView = (WebView) findViewById(R.id.webView); webView.clearCache(true); webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(LOG_TAG, message); new AlertDialog.Builder(view.getContext()) .setMessage(message).setCancelable(true).show(); result.confirm(); return true; } }); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { Toast.makeText(view.getContext(), "onPageStarted", Toast.LENGTH_SHORT).show(); super.onPageStarted(view, url, favicon); //To change body of generated methods, choose Tools | Templates. } @Override public void onPageFinished(WebView view, String url) { Toast.makeText(view.getContext(), "onPageFinished", Toast.LENGTH_SHORT).show(); super.onPageFinished(view, url); //To change body of generated methods, choose Tools | Templates. } }); WebSettings webSettings = webView.getSettings(); webSettings.setBuiltInZoomControls(true); webSettings.setJavaScriptEnabled(true); webSettings.setLoadWithOverviewMode(true); webSettings.setAllowContentAccess(true); webSettings.setUseWideViewPort(true); webView.loadUrl("http://192.168.1.236:8080/mobile/android.html"); }
and the android.html have ajax request. the android application works fine and load the android.html but without getting the ajax data