3

I have found question to read content from webview.I have tried i can't able to fix.

I have displayed a html page in webview. I need the content of that html page in a string

java code

WebView mainContent = (WebView)layout.findViewById(R.id.webView1);
         mainContent.getSettings().setJavaScriptEnabled(true);
         WebSettings webSettings = mainContent.getSettings();
         webSettings.setJavaScriptEnabled(true);
         webSettings.setBuiltInZoomControls(true);
         mainContent.requestFocusFromTouch();
          mainContent.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
                }
            });
         mainContent.setWebChromeClient(new WebChromeClient());
         mainContent.loadUrl("file:///android_asset/"+filename.get(position));
         webSettings.setDefaultFontSize(40);

And in MY Activity

public void processHTML(String html) {

                System.out.println("======++++"+Html.fromHtml(html));
            }

I didn't see my anything log. How can i get the content(text) of HTML page in a String. What im doing mistake? Thanks in advance

Make it Simple
  • 1,832
  • 5
  • 32
  • 57

2 Answers2

3

I got the content by this way

mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
mainContent.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        mainContent.setWebViewClient(null);    
        mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<div>'+document.getElementsByTagName('div')[0].innerHTML+'</div>');");

and in my activity

class MyJavaScriptInterface {
    @SuppressWarnings("unused")
    public void processHTML(final String html) {
        runOnUiThread(new Runnable() {
            public void run() {
                Spanned page = Html.fromHtml(html);
                System.out.println("content"+page);
            }
        });
    }
}
Make it Simple
  • 1,832
  • 5
  • 32
  • 57
1

Have you tried to launch a GET petition to that page?

responseString is the HTML page in a String. For example:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

Launch AsyncTask like:

try {
        String page = new RequestTask().execute("http://myurl.com").get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
SuppressWarnings
  • 4,134
  • 4
  • 25
  • 33