如何json解析android webview中的url参数?

时间:2016-04-08 11:31:00

标签: android webview android-webview android-json android-json-rpc

我有一个webview,我在页面完成加载(onpagefinished)后注入javascript,如下所示:

@Override
                    public void onPageFinished(WebView view, String url) {
String jscontent = "";
                        try{
                            AssetManager manager = view.getContext().getAssets();
                            InputStream is = manager.open("page.js"); 
                            InputStreamReader isr = new InputStreamReader(is);
                            BufferedReader br = new BufferedReader(isr);

                            String line;
                            while (( line = br.readLine()) != null) {
                                jscontent += line;

                            }
                                is.close();
                            }
view.loadUrl("javascript:" + jscontent);
}

这是我正在阅读的javascript页面: page.js:

if (window.page) {
        return; // page has been loaded from somewhere, not to redefine it again
    }
var _$p = window.page = {
            version: "0.1"
        };
function Response(xhr) {
        this.xhr = xhr;
        this.status = xhr.status;
    }
    Response.prototype.getHeader = function(header) {
        return this.xhr.getResponseHeader(header);
    };
    Response.prototype.getBody = function() {
        return this.xhr.responseText;
    };

    function ajax(url, callback) {
        //TODO support PUT method
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.onerror = function(e) {
            callback.onerror(e);
        };
        xhr.onreadystatechange = function(event) {
            if (this.readyState != this.DONE || this.status == 0) { 
                return;
            }
            if (this.status != 200) {
                var response = new Response(xhr);
                callback.onfail(response);
                return;
            }
            callback.onsuccess(this.responseText);
        };
        xhr.send();
    };


    var rpc = {
        type: ["SMALL", "LARGE"],
        exec: function(call, type) {// "call" conform to JSON-RPC 2.0 
            //TODO differentiate POST/GET based on rpc.type.SMALL/LARGE
            var param = encodeURIComponent(JSON.stringify(call));
            ajax("?_$p=" + param, {
                onerror: function(e) {
                    //TODO use logging
                    alert("Error:"+e.toString());
                },
                onfail: function(response) {
                    //TODO use logging
                    alert("Failed:"+response.getBoby());
                },
                onsuccess: function(text) {
                    //TODO use logging
                    alert("Page success:"+text);
                },
            });
        }
    };
_$p.navigate = function(url, title) {
            rpc.exec(// JSON-RPC 2.0
                {method: "navigate", params: {
                    "url": url, 
                    "title": title
                }}, rpc.type.SMALL);
        };

如何在javascript中解析导航功能的url参数的json值。 json的考试:{"方法":"导航","参数":{" url":" {{3} }"," title":"某些网址"}}。 另外,在webview的shouldinterceptrequest方法下进行它是否理想?

谢谢!

1 个答案:

答案 0 :(得分:-1)

我不太确定你想要达到的目的但是......

根据您提供的代码:

.... 

 var _$p = window.page = {
        version: "0.1"
    };

 //create variables to store your "local variables"
 var local_url = "";
 var local_title = "";

function Response(xhr) {
    this.xhr = xhr;
    this.status = xhr.status;
}

 ....

_$p.navigate = function(url, title) {
        rpc.exec(// JSON-RPC 2.0
            {method: "navigate", params: {
                "url": url, 
                "title": title
            }}, rpc.type.SMALL);

      //save your variables into your "local variables"
       local_url = url;
       local_title = title;
};

注意:变量仅在调用$ p.navigate后才可用。否则,它将是一个空字符串。