Pawanpreet
Pawanpreet

Reputation: 355

Ajax not working in android webview

I am loading a website in webview,we have used Ajax in website and its working fine on web browser and mobile browser also but in android webview ajax is not working no error is there in the console.Here is my code:-

public class Activity_WebView extends AppCompatActivity implements  
 ConnectivityReceiver.ConnectivityReceiverListener {
WebView webview;
ProgressDialog pro_dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    webview.setWebViewClient(new loadinsame());
    pro_dialog = new ProgressDialog(Activity_WebView.this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setAllowUniversalAccessFromFileURLs(true);

    boolean connection = checkConnection();
    if (connection) {
        webview.loadUrl("website url");
    } else {
        Toast.makeText(Activity_WebView.this, "Sorry! Not connected to 
       internet", Toast.LENGTH_SHORT).show();
        dialog_Show(webview, "Please check you Inernet connect and Reload.", 
        false);
    }
}

@Override
public void onNetworkConnectionChanged(boolean isConnected) {
    if (!isConnected) {
        Toast.makeText(Activity_WebView.this, "Sorry! Not connected to 
        internet", Toast.LENGTH_SHORT).show();
    }
}

private class loadinsame extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        pro_dialog.setCancelable(false);
        pro_dialog.setMessage("Loading...");
        pro_dialog.show();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        pro_dialog.dismiss();

    }

    @Override
    public void onReceivedError(final WebView webview, WebResourceRequest 
    request, WebResourceError error) {
        super.onReceivedError(webview, request, error);
        pro_dialog.dismiss();
       // dialog_Show(webview, "Error Occur, Do you want to Reload?", true);

    }
}

@Override
public void onBackPressed() {

    if (webview.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
    }
}

private boolean checkConnection() {
    boolean isConnected = ConnectivityReceiver.isConnected();
    return isConnected;
}

@Override
protected void onResume() {
    super.onResume();
    MyApplication.getInstance().setConnectivityListener(this);
}
}

when i inspect the website in chrome using emulator, find that my ajax remain pending and then cancel after sometime. Thanks in advance.

Upvotes: 8

Views: 14364

Answers (5)

Albert.Qing
Albert.Qing

Reputation: 4615

Update jquery‘s version

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

Upvotes: 0

Mohammed Tallat
Mohammed Tallat

Reputation: 1

Here is how to edit the AndroidManifes.xml:

Find AndroidManifest.xml file in application folder at:
android/app/src/main/AndroidManifest.xml
Locate the application subelement. Add the following tag:
android:usesCleartextTraffic=”true”
The application subelement should now look like:

<application
    android:name=”io.flutter.app.Test”
    android:label=”bell_ui”
    android:icon=”@`enter code 
    here`mapmap/ic_launcher”
    android:usesCleartextTraffic=”true”>

Save the AndroidManifest.xml file.

Upvotes: 0

Er.Prem Singh daksha
Er.Prem Singh daksha

Reputation: 393

Solutions

  1. If you want to load web url in WebView so you need follow my code It's code working fine and In web any type of script language used in web page.

     public void webviewCallBack(String coverUrl) {
        WebView  WebView  webView = (WebView) findViewById(R.id.web_view);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView.getSettings().setPluginState(WebSettings.PluginState.ON);
            webView.getSettings().setAppCacheEnabled(true);
            webView.getSettings().setDatabaseEnabled(true);
            webView.getSettings().setDomStorageEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.clearView();
            webView.measure(100, 100);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
    
            webView.setWebViewClient(new WebViewClient() {
    
                // For api level bellow 24
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.d("weburl__", url);
    
                    if (url.startsWith("http") || url.startsWith("https")) {
                        // Return false means, web view will handle the link
                        return false;
                    } else if (url.startsWith("tel:")) {
                        // Handle the tel: link
                        handleTelLink(url);
    
   // Return true means, leave the current web view and handle the url itself
                        return true;
                    }
    
                    return false;
                }
    
                // From api level 24
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    // Get the tel: url
                    String url = request.getUrl().toString();
                    Log.d("weburl_____", url);
    
                    if (url.startsWith("http") || url.startsWith("https")) {
                   // Return false means, web view will handle the link
                        return false;
                    } else if (url.startsWith("tel:")) {
                        // Handle the tel: link
                        handleTelLink(url);
    
                        // Return true means, leave the current web view and handle the url itself
                        return true;
                    }
    
                    return false;
                }
            });
    
            // Load the url into web view
            webView.loadUrl(coverUrl);
            hoadler();
        }

    public void hoadler() {


        Runnable task = new Runnable() {
            public void run() {
                material_design_progressbar.setVisibility(View.VISIBLE);
            }
        };
        worker.schedule(task, 5, TimeUnit.SECONDS);
    }

  1. I hope this code is helpful for you!

Upvotes: -1

Master Mind
Master Mind

Reputation: 2406

When i enabled the java script, its working.

 WebView myWebView = (WebView) findViewById(R.id.webview);
 myWebView.loadUrl("");
 myWebView.getSettings().setDomStorageEnabled(true);
 myWebView.getSettings().setJavaScriptEnabled(true);
 myWebView.setWebViewClient(new WebClient());

Upvotes: 0

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

Just promoting @Jeff Thomas comment, by setting

mWebView.getSettings().setDomStorageEnabled(true);

worked!!

Upvotes: 8

Related Questions