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
Render Error in Android Studio 3.0 Layout Editor
App Completely Restarting When Launched by Icon Press in Launcher
How to Check Internal and External Storage If Exist
Set Permission for Getting User's Email Id from Facebook Login
Message "Error: Resource Android:Attr/Lstar Not Found"
Android 11 Action_Open_Document_Tree: Set Initial Uri to the Documents Folder
Show Current Location Inside Google Map Fragment
Why Getapplicationcontext() in Constructor of Activity Throws Null Pointer Exception
Runonuithread VS Looper.Getmainlooper().Post in Android
Get the Progress Time of the Video Played Under Videoview
Need to Disable Expand on Collapsingtoolbarlayout for Certain Fragments
If I Call Getmeasuredwidth() or Getwidth() for Layout in Onresume They Return 0
Fragment Replacing in Recyclerview Item
Detect When Android V2 Maps Has Loaded
Android Unknown Command 'Crunch'