How to Get HTML Source Code from Url in Android

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?

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

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.

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


Related Topics



Leave a reply



Submit