How do I navigate within a webview with the help of a locally imported js file?

爷,独闯天下 提交于 2019-12-24 01:05:53

问题


I am working on a webview that calls a url from webserver, such that url = abc.com/thispage which is a http: link Now inside that this page, i am navigating futher to another page ,say "mainpage", i would like to import a local js file which I have written such that: steps:

1) I import the myjspage.js file to the project (done in assets )

2) Append "_$page_on=1" parameter to the requested url.

3) Read data from myjspage.js file and inject into webcontrol after page is loaded.

4) Intercept url request inside webview control and search for '_$page" url parameter

5) Parse json value of parameter for next set of instructions.Example of json :{"method": "navigate","params":{"url": "http://some.url","title": "Some Url”}}.l

I have already achieved steps 1 thru 4 : as follows:

1) imported js file in assets as expected

2)

url = "abc.com/thispage"
        StringBuffer buffer=new StringBuffer(url);
        buffer.append("?_$page_on=1");
        return buffer.toString();

3) In onpagefinished of my webview :

@Override
                    public void onPageFinished(WebView view, String url) {

                                                String jscontent = "";
                        try{
                            AssetManager manager = view.getContext().getAssets();
                            InputStream is = manager.open("myjspage.js");
                            InputStreamReader isr = new InputStreamReader(is);
                            BufferedReader br = new BufferedReader(isr);

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

                            }
                            is.close();
                        }

                        catch(Exception e){}

                        view.loadUrl("javascript:(" + jscontent + ")()");

                    }

4) In

 @Override
                            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if(url.contains("_$page")){
                             getData();

                            }

}

5)where getData() is :

public void getData()
    {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpGet request = new HttpGet(getUrl());
        JSONObject jsonObject;
        String jsonString = "{\"json\":{\"method\":\"navigate\",\"params\":[{\"url\":\"mypage://realpage/data=CompanyData/its=goog.o"},{\"title\":\"Company name\"}]}}";


        try
        {
            jsonObject = new JSONObject(jsonString);
            JSONObject itemObject = jsonObject.getJSONObject("json");
            String jsonName = "method: " +itemObject.getString("method");
            JSONArray urlarray = itemObject.getJSONArray("params");

        }catch(Exception e)
        {
            e.printStackTrace();
        }

        httpclient.getConnectionManager().shutdown();
    }

For step5 I am not sure if what I am doing is the right way to do it. Infact I am unsure if I am missing something for steps 2-4 aswell. Based on the the fact that I should be able to navigate to call a native fragment upon the click of a recognized row within the "mainpage" section of the webview. Has anyone dealt with handling local actions and json parsing in webview with respect to js and can shed some light ? Will be really helpful, thanks!

来源:https://stackoverflow.com/questions/36454232/how-do-i-navigate-within-a-webview-with-the-help-of-a-locally-imported-js-file

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