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

Time:04-05

In my Kotlin Android application I want to do the following:

  1. Get the HTML source of a URL
  2. 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()
  • Related