Android/Kotlin
Jsoup
lipnus
2018. 11. 26. 23:16
반응형
1. Gardle
implementation 'org.jsoup:jsoup:1.11.3'
공식사이트: https://jsoup.org/
2. Manifest
<uses-permission android:name="android.permission.INTERNET"/>
3. Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// testJsoup()
selectTest()
}
fun selectTest(){
object : Thread() {
override fun run() {
val response = Jsoup.connect("http://www.google.com")
.method(Connection.Method.GET)
.execute()
val googleDocument = response.parse()
val btnK = googleDocument.select("input[name=btnK]").first()
val btnKValue = btnK.attr("value")
Log.d("SSS", btnKValue)
}
}.start()
}
fun testJsoup(){
object : Thread() {
override fun run() {
// 간략화된 GET, POST
val google1 = Jsoup.connect("http://www.google.com").get()
// val google2 = Jsoup.connect("http://www.google.com").post()
// Response로부터 Document 얻어오기
val response = Jsoup.connect("http://www.google.com")
.method(Connection.Method.GET)
.execute()
val document = response.parse()
val html = document.html()
val text = document.text()
//ui를 위한 Thread
runOnUiThread {
tv1.setText("html: " + html)
tv2.setText("text: " + text)
}
}
}.start()
}
}
반응형