Update MainActivity.kt

This commit is contained in:
MNCHL 2023-12-25 23:18:15 +08:00 committed by GitHub
parent 48e6c84b15
commit 5d321a8b0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,7 +70,34 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
override var themeId: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
fetchData()
private fun fetchData() {
val client = OkHttpClient()
val url = "http://mkoc.cn/aip/version.php"
val request = Request.Builder()
.url(url)
.build()
// 使用协程来执行网络请求
GlobalScope.launch(Dispatchers.IO) {
try {
val response: Response = client.newCall(request).execute()
if (response.isSuccessful) {
val responseBody = response.body?.string()
Log.d("MainActivity", "Response: $responseBody")
// 在这里处理响应数据,可以更新 UI
// 例如,你可以使用 runOnUiThread 来更新 UI 控件
} else {
Log.e("MainActivity", "Request failed with code: ${response.code}")
}
} catch (e: Exception) {
Log.e("MainActivity", "Error: ${e.message}")
}
}
}
}
val updateManager = UpdateManager(this)
updateManager.checkForUpdates()
val splashScreen = installSplashScreen()
@ -114,35 +141,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
)
}
private fun fetchData() {
val client = OkHttpClient()
val url = "http://mkoc.cn/aip/version.php"
val request = Request.Builder()
.url(url)
.build()
// 使用协程来执行网络请求
GlobalScope.launch(Dispatchers.IO) {
try {
val response: Response = client.newCall(request).execute()
if (response.isSuccessful) {
val responseBody = response.body?.string()
Log.d("MainActivity", "Response: $responseBody")
// 在这里处理响应数据,可以更新 UI
// 例如,你可以使用 runOnUiThread 来更新 UI 控件
} else {
Log.e("MainActivity", "Request failed with code: ${response.code}")
}
} catch (e: Exception) {
Log.e("MainActivity", "Error: ${e.message}")
}
}
}
}
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setUpNavigation(navHostFragment.navController)