Skip to content

Instantly share code, notes, and snippets.

@happysingh23828
Created January 15, 2021 13:02
Show Gist options
  • Save happysingh23828/815a28f49c3e11f845b9f954ca4505f3 to your computer and use it in GitHub Desktop.
Save happysingh23828/815a28f49c3e11f845b9f954ca4505f3 to your computer and use it in GitHub Desktop.
Utils for getting the HTML code from any website or Weblink. (Android)
import androidx.appcompat.app.AppCompatActivity
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.MalformedURLException
import java.net.URL
object ContentScrapper {
fun getHTMLData(activity: AppCompatActivity,url: String, scrapListener: ScrapListener) {
Thread(Runnable {
val google: URL?
val `in`: BufferedReader?
var input: String?
val stringBuffer = StringBuffer()
try {
google = URL(url)
`in` = BufferedReader(InputStreamReader(google.openStream()))
while (true) {
if (`in`.readLine().also { input = it } == null)
break
stringBuffer.append(input)
}
`in`.close()
activity.runOnUiThread {
scrapListener.onResponse(stringBuffer.toString())
}
} catch (e: MalformedURLException) {
e.printStackTrace()
activity.runOnUiThread {
scrapListener.onResponse(null)
}
}
}).start()
}
interface ScrapListener {
fun onResponse(html: String?)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment