3

i want to get the html code of a webpage in android. The webpage url will be given in a edit textbox then when the user will click the button a text view will show the code of that webpage. Please explain and give the code!

Any help would be appreciated!

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113

3 Answers3

2
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    str.append(line);
}
in.close();
html = str.toString();

Don't forget to add the internet permission in the AndroidManifest:

<uses-permission android:name="android.permission.INTERNET" /> 

You can refer to these links for more help:

http://lexandera.com/2009/01/extracting-html-from-a-webview/

Is it possible to get the HTML code from WebView

How to get the html-source of a page from a html link in android?

Community
  • 1
  • 1
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • How i get just some line of html-source code of webpage ? if i get full source page it may get so much time and i dont need to get all line, can you help me? thx very much – Milad gh Sep 17 '15 at 04:07
1

You need a HttpClient to perform a HttpGet Request. Then you can read the content of that request.

This snippet gives you an InputStream:

  public static InputStream getInputStreamFromUrl(String url) {
  InputStream content = null;
  try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(url));
    content = response.getEntity().getContent();
  } catch (Exception e) {
    Log.e("[GET REQUEST]", "Network exception", e);
  }
    return content;
}

And this method returns the String:

// Fast Implementation
private StringBuilder inputStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    // Read response until the end
    while ((line = rd.readLine()) != null) { 
        total.append(line); 
    }

    // Return full string
    return total;
}

Source: http://www.androidsnippets.com/executing-a-http-get-request-with-httpclient and http://www.androidsnippets.com/get-the-content-from-a-httpresponse-or-any-inputstream-as-a-string

Kiril
  • 6,009
  • 13
  • 57
  • 77
  • can you tell how to show that html code into a textview? i tried it it didn't worked! It stopped unexpectedly! @Kiril – Mohammad Areeb Siddiqui Jun 05 '12 at 19:51
  • 1
    I assume you have defined a TextView in your layout. Now you can access you TextView by using: `TextView yourTextView = (TextView) findViewById(R.id.NameOfYourTextView);` After that you can call the method `yourTextView.setText("The HTML code");` EDIT: Also check out the answer below for extractng HTML code – Kiril Jun 05 '12 at 20:15
  • @Kiril - How i get just some line of html-source code of webpage ? if i get full source page it may get so much time and i dont need to get all line, can you help me? thx very much – Milad gh Sep 17 '15 at 04:08
-1

Use the code above, and set it to a textview like this:

InputStream is =InputStream getInputStreamFromUrl("http://google.com");
String htmlText = inputStreamToString(is);

mTextView.setText(Html.fromHtml(htmlText));

but do the network request in a separate thread/asynctask :)

Zelleriation
  • 2,834
  • 1
  • 23
  • 23