In my Kotlin Android application I want to do the following:
- Get the HTML source of a URL
- Get the URL from the first
img
tag inside the HTML source that was retrieved
How should I go about doing this? Is there a library available for Android where you can send a URL and get the HTML source in return?
CodePudding user response:
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()