EnthuDeveloper
EnthuDeveloper

Reputation: 670

How to get the HTML code of the page that would be loaded in webview?

I have an android application in which I need to get the HTML code of the page that will be loaded in the webview. This code I need to get it even before the page loads in the webview and act accordingly.

I am trying to capture the URL that would be loaded in the method

"public void onPageStarted(WebView view, String url, Bitmap favicon)"

But what is the way to get the HTML code of the page that would be loaded in the webview?

Upvotes: 0

Views: 1706

Answers (3)

user849998
user849998

Reputation:

Utilizing HttpClient:

public String httpGet(String url) throws URISyntaxException,
       ClientProtocolException, IOException {
      try {

       HttpGet request = new HttpGet();

       request.setURI(new URI(url));
       response = client.execute(request);

       htmlBody = EntityUtils.toString(response.getEntity());

      } catch (Exception ex) {
      }

      return htmlBody;
     }

And there are two ways of showing it in the WebView, either from file (then you are supposed to save a file with html on th device, which makes the content to be available offline):

myWebView.loadUrl("file://"
                                + Environment.getExternalStorageDirectory()
                                + "/filename.html");

or you feed WebView a string with html:

myWebView.loadData(HTMLString, "text/html", "utf-8");

Here you may find the whole httpClient written by me, some functions you may need, some not:

http://droidsnip.blogspot.com/2011/10/custom-http-client-android.html#more

Upvotes: 2

Arslan Anwar
Arslan Anwar

Reputation: 18746

I don't found any webview function to get HTML data. I found this useful.

 try {
            URL twitter = new URL(
                    "http://www.stackoverflow.com");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String htmlData = "";
            String line;
            while ((line = in.readLine()) != null) {
                   htmlData = htmlData+"\n"+line;
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 0

user370305
user370305

Reputation: 109237

I don't know for how to get webview content, but I am using this for code of webpage

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("website url");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
      response.getEntity().getContent()
    )
  );

String line = null;
while ((line = reader.readLine()) != null){
  result += line + "\n";

}

The second method is,

 URL url;
    try {
        // url = new URL(data.getScheme(), data.getHost(), data.getPath());
        url = new URL("website url");
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                url.openStream()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            text.append(line);
        }

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

Upvotes: 0

Related Questions