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()

}
}


반응형

'Android > Kotlin' 카테고리의 다른 글

그래픽 라이브러리 없이 Retrofit만 사용하여 이미지 출력  (0) 2018.11.28
WebView  (0) 2018.11.27
[문법] While문에서 assignment 불가능  (0) 2018.11.25
kotlin기본 문법  (0) 2018.11.25
[Kotlin] HashMap  (0) 2018.11.24