Ajax doesn’t work in Android WebView

I'm loading a website in WebView. We use ajax on the website. It also works well in web browsers and mobile browsers, but Ajax doesn't work properly in Android WebView, and there are no errors in the console. This 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 used the simulator to check the website in chrome, I found that my Ajax remained suspended, and then cancelled after a period of time. Thank you in advance

resolvent:

Blocking your Ajax calls by overriding shouldinterceptrequest is as follows:

private class LoadInSame extends WebViewClient {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView webview, WebResourceRequest webrequest)
    {
        Log.d("test", "shouldInterceptRequest");
       return this.handleRequest(webrequest.getUrl().toString());
    }
    @NonNull
    private WebResourceResponse handleRequest(@NonNull String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("User-Agent", "");
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.connect();

            InputStream inputStream = connection.getInputStream();
            return new WebResourceResponse("text/json", "utf-8", inputStream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        catch (ProtocolException e) {
            e.printStackTrace();
            return null;
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}

Please also follow the Java Naming Convention

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>