Created
January 15, 2021 13:02
-
-
Save happysingh23828/815a28f49c3e11f845b9f954ca4505f3 to your computer and use it in GitHub Desktop.
Utils for getting the HTML code from any website or Weblink. (Android)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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