Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
Pawanpreet Avatar asked Nov 23 '16 05:11

Pawanpreet


1 Answers

Just promoting @Jeff Thomas comment, by setting

mWebView.getSettings().setDomStorageEnabled(true);

worked!!

like image 105
Suraj Vaishnav Avatar answered Oct 31 '22 19:10

Suraj Vaishnav