0

So this may seem like a really simple question, but I haven't had any luck searching for it because it's also kind of specific.

I have a website, which i use WebView, i called wv, to load it via wv.load(URL)

On the website, there are php forms, which will generate html tables when the user interacts with it (by clicking submit).

Since html table was generated, the source code for the html table was also generated, but the url stays the same. The newly generated source code that has html table in it is the one that i want to grab.

Please see my code below:

 private final String URL = "http://google.com";

private WebView wv;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv = (WebView) findViewById(R.id.wvPage);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(URL);

    wv.setWebViewClient(new WebViewClient()
    {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            //view.loadUrl(URL);
            new AsyncClass().execute(url);
            Log.i("ASDF", "pagefinished");

        }
    });

}

I called the execute function everytime the form finish loading. Even with the tables generated, i still dont get the newly generated source code for the table. Below is my AsynTask implementation: I just copied it from another post.

@Override
protected Void doInBackground(String... URL)
{
    try
    {
        HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
        HttpGet httpget = new HttpGet(URL[0]); // Set the action you want to do
        HttpResponse response = httpclient.execute(httpget); // Execute it
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent(); // Create an InputStream with the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
            Log.i("Source Code:", line);
        }
        String resString = sb.toString(); // Result is here
        is.close();
    }
    catch (Exception ex){
        ex.printStackTrace();
        Log.i("ERROR", ex.toString());
    }
    return null;
}

All I want to do is get the new source code of the html table, so I can move on to parsing it. Any help is appreciated.

Vibol
  • 615
  • 1
  • 8
  • 25
  • The html tables were generated because parameters were send by POST or GET action to that url. You are executing a GET on that url without parameters. – greenapps Apr 25 '15 at 21:01
  • Thanks for your reply but my questions seems to be premature. I just found a solution a couple of minutes ago. – Vibol Apr 25 '15 at 21:09
  • Yes. Ok. But the solution does not grab the source from the url anymore as your subject states. But from a webview. – greenapps Apr 26 '15 at 09:16
  • @greenapps Yes you are correct. Grabbing the source code from webview is also what i want. Additionally, it is also a lot simpler than the other approach :) – Vibol Apr 26 '15 at 17:21

1 Answers1

1

So I did a little bit more of digging and found the solution in another thread. I did not even have to use httprequest. Just need a little bit of help from javascript.

Here's a link to the thread that helped me.

Here's my new code.

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv = (WebView) findViewById(R.id.wvPage);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    wv.loadUrl(URL);

    wv.setWebViewClient(new WebViewClient()
    {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            wv.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        }
    });

}

class MyJavaScriptInterface
{
    @JavascriptInterface
    @SuppressWarnings("unused")
    public void processHTML(String html)
    {
        // process the html as needed by the app
        String[] lines = html.split("\\n");
        for (String line : lines)
        {
            Log.i("Source Code: ", line);
        }
    }
}
Community
  • 1
  • 1
Vibol
  • 615
  • 1
  • 8
  • 25