Memo

メモ > 技術 > IDE: AndroidStudio > アプリの作成(Jetpack Compose / HTMLを取得する)

■アプリの作成(Jetpack Compose / HTMLを取得する)
【Android Kotlin】OkHttp3でコピペ可能なAPI通信処理実装 - 都市語〜トシカタ〜 福岡発、都市を語る場所 https://japanesecitylikers.com/?p=1426 [Kotlin] OkHttp3の使い方 | GETとPOSTリクエスト | とろなび | プログラミング系備忘録 https://toronavi.com/connection-okhttp Overview - OkHttp https://square.github.io/okhttp/ マニフェストファイルを編集し、インターネットに接続できるようにする 追加場所は、ルートであるmanifestの直下でいい manifests/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
build.gradle の dependencies 内に以下を追加する
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
以下のようにプログラムを作成する
@Composable fun MainScreen() { var title by remember { mutableStateOf("") } LaunchedEffect(Unit) { withContext(Dispatchers.IO) { try { val client = OkHttpClient() val request = Request.Builder() .url("https://www.example.com/") .build() val response = client.newCall(request).execute() val text = response.body?.string() ?: "" val titleRegex = "<title>(.*?)</title>".toRegex() val matchResult = titleRegex.find(text) title = matchResult?.groups?.get(1)?.value ?: "Title not found" Log.d("OK", title) } catch (e: Exception) { Log.d("ERROR", "OkHttpClient") } } } Column { Text(title) } }

Advertisement