2

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!

  • Your getData method looks very strange - the Http request is never called and the parsed json is never actually used. Could explain in more details what do you expect to happen. – Samuil Yanovski Apr 13 '16 at 16:14
  • I am not using it anymore, it was just for testing. I just need to retrieve the json "url" parameter from the javascript under "navigate " function. so I can pass it to a local java variable –  Apr 13 '16 at 19:28
  • Also, I am unable to read the javascript when I read it thru an inputstream buffer. How do I read it ? It works only when I hardcode it in the code itself. Is there something to counteract the tabs and new lines that might be obstructing this? –  Apr 13 '16 at 19:29

0 Answers0