来自webview中自定义html的Ajax调用没有使用shouldInterceptRequest进行调用

时间:2014-02-14 16:00:03

标签: android ajax webview

我有这个简单的应用程序,我在其中加载一个包含按钮上的ajax调用的HTML的WebView。 我希望我的函数'shouldInterceptRequest'捕获该请求并发回一些东西(现在是一个字符串,但它将是一个)

以下是我的WebViewActivity代码:

公共类WebViewActivity扩展了Activity {

private WebView webView;
private Uri picUri;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    //webView.loadUrl("http://www.google.com");


    String newHTML = "<!doctype html><html><head>"+
    "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\"></script>"+
    "<script>"+
        "function myCall() {"+
            "var request = $.ajax({"+
                "url: \"ajax.php\","+
                "type: \"GET\","+            
                "dataType: \"html\""+
            "});"+
            "request.done(function(msg) {"+
                "$(\"#mybox\").html(msg);"+          
            "});"+

            "request.fail(function(jqXHR, textStatus) {"+
                "alert( \"Request failed: \" + textStatus );"+
            "});"+
        "}"+
    "</script>"+
            "<meta charset=\"utf-8\" />"+
            "<title>My jQuery Ajax test</title>"+
            "<style type=\"text/css\">"+
                "#mybox {"+
                    "width: 300px;"+
                    "height: 250px;"+
                    "border: 1px solid #999;"+
                "}"+
            "</style>"+
        "</head>"+
        "<body>"+
            "The following div will be updated after the call:<br />"+
            "<div id=\"mybox\">"+
            "</div>"+
            "<input type=\"button\" value=\"Update\" />"+
        "</body>"+
    "</html>";



    webView.loadData(newHTML, "text/html", "UTF-8");


    webView.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {


                return super.shouldInterceptRequest(view, url+" captured");

        }

    });
}
}

出于某种原因,我没有捕获那个ajax调用。 我做错了什么?。

1 个答案:

答案 0 :(得分:0)

清单是我的问题!我没有网络部分!所以我所做的就是在清单中,最后添加:

元网络: *

所以它的工作方式是,无论我在NETWORK被缓存之前有什么,如果没有东西,就没有办法知道该怎么做。有了这个陈述,我们在缓存中没有,我们从NETWORK

获取它