Ajax在android webview中不起作用

我正在webview中加载一个网站,我们在网站上使用了Ajax,它在网页浏览器和移动浏览器上也运行良好,但在android webview中ajax无法正常工作,控制台中没有错误.这是我的代码: –

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);
}
}

当我使用模拟器检查chrome中的网站时,发现我的ajax保持挂起状态,然后在一段时间后取消.
提前致谢.

解决方法:

通过覆盖shouldInterceptRequest拦截你的ajax调用是这样的:

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;
        }

    }
}

还请遵循JAVA命名约定.

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...