如何在 Android 中使用 Kotlin 解析 HTML?
本示例演示了如何在 Android 中使用 Kotlin 解析 HTML。
步骤 1 − 在 Android Studio 中创建一个新项目,转至文件 ⇒ 新项目,并填写所有必需的详细信息以创建一个新项目。
步骤 2 − 向 res/layout/activity_main.xml 添加以下代码。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <Button android:id="@+id/btnParseHTML" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="Get website" /> <TextView android:textColor="@android:color/background_dark" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btnParseHTML" android:layout_centerHorizontal="true" android:text="Result" android:textSize="12sp" android:textStyle="bold" /> </RelativeLayout>
步骤 3 − 向 build.gradle(模块:应用)添加给定的依赖关系
implementation 'org.jsoup:jsoup:1.11.2'
步骤 4 − 向 src/MainActivity.kt 添加以下代码
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements
import java.io.IOException
class MainActivity : AppCompatActivity() {
lateinit var button: Button
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
button = findViewById(R.id.btnParseHTML)
button.setOnClickListener {
getHtmlFromWeb()
}
}
private fun getHtmlFromWeb() {
Thread(Runnable {
val stringBuilder = StringBuilder()
try {
val doc: Document = Jsoup.connect("https://tutorialspoint.com/").get()
val title: String = doc.title()
val links: Elements = doc.select("a[href]")
stringBuilder.append(title).append("
")
for (link in links) {
stringBuilder.append("
").append("Link :
").append(link.attr("href")).append("
").append("Text : ").append(link.text())
}
} catch (e: IOException) {
stringBuilder.append("Error : ").append(e.message).append("
")
}
runOnUiThread { textView.text = stringBuilder.toString() }
}).start()
}
}步骤 5 − 向 androidManifest.xml 添加以下代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.q11"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们试着运行你的应用程序。我假设你已经将实际的 Android 移动设备与电脑相连。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后从工具栏中单击运行图标
。选择你的移动设备作为选项,然后检查你的移动设备,它将显示你的默认屏幕


广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP