How to Get the HTML Source of a Page from a HTML Link in Android

How to get the HTML source of a page from a HTML link in Android?

You can use HttpClient to perform an HTTP GET and retrieve the HTML response, something like this:

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();

How do I fetch the HTML source of a URL in Android?

Get the HTML source of a URL

Use any HTTP client API. I recommend OkHttp, but there are plenty of others.

Get the URL from the first img tag inside the HTML source that was retrieved

Parse the HTML using an HTML parser, and use the parsed result to find your desired HTML tag. JSoup is fairly popular, and it also happens to include an HTTP client, which you might use instead of OkHttp or anything else.

You would wind up with something like:

val doc = Jsoup.connect("YOUR URL GOES HERE").get()
val firstImg = doc.select("img").first()

android - how to get html code of a webpage in android?

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?

How to access HTML source from Android WebView?

I found my quesntion's solution in this post.

https://stackoverflow.com/questions/6503574/how-to-get-html-source-code-from-url-in-android[][1]

I don't know much about Ion dependency but it did my work.

Get HTML code from url in android

Try this (wrote it from the hand)

URL google = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(google.openStream()));
String input;
StringBuffer stringBuffer = new StringBuffer();
while ((input = in.readLine()) != null)
{
stringBuffer.append(input);
}
in.close();
String htmlData = stringBuffer.toString();

Get Generated HTML On Android

The solution that worked for the OP in Android Studio to get the source code of an external website after Javascript had run is the code from this other Stackoverflow question How do I get the web page contents from a WebView?

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
// process the html as needed by the app
}
}

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});

/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");


Related Topics



Leave a reply



Submit