How to json parse a url parameter in android webview?

有些话、适合烂在心里 提交于 2019-12-02 23:02:23

问题


I have a webview where I am injecting javascript after page has finished loading (onpagefinished)as follows:

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

This is the javascript page I am reading: 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);
        };

How do I parse the json value of the url parameter for navigate function in javascript. Examlpe of json: {"method": "navigate","params":{"url": "http://some.url","title": "Some Url"}}.? Also, Would it be ideal to do it under shouldinterceptrequest method of webview?

Thanks!


回答1:


I am not too sure what you are trying to achieve but..

Based on your given code:

.... 

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

Note: The variables will only be available after $p.navigate is called. Otherwise, it will be an empty string.



来源:https://stackoverflow.com/questions/36498500/how-to-json-parse-a-url-parameter-in-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!