feat: 网络

This commit is contained in:
2025-11-12 18:20:57 +08:00
parent 22ff4384ca
commit 570de3c4e7
24 changed files with 600 additions and 11 deletions
@@ -7,9 +7,20 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.yuanxuan.rokid.dependencies.AppDependencies
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yuanxuan.rokid.network.http.ApiResponse
import com.yuanxuan.rokid.network.http.Test
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import timber.log.Timber
import java.lang.reflect.Type
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -21,9 +32,26 @@ class MainActivity : AppCompatActivity() {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
AppDependencies.deviceServiceManager.playTTS("测试一下")
val json = "{\n" +
" \"code\": 0,\n" +
" \"msg\": \"success\",\n" +
" \"data\": {\n" +
" \"userId\": \"123\",\n" +
" \"name\": \"Alice\"\n" +
" }\n" +
"}"
val x = test<Test>(json)
// onBackPressedDispatcher.addCallback {
// }
}
private inline fun <reified T> test(responseBody: String): T {
val typeToken = object : TypeToken<ApiResponse<T>>() {}
val typeOfT: Type = typeToken.type
val apiResponse: ApiResponse<T> = Gson().fromJson(responseBody, typeOfT)
return apiResponse.data
onBackPressedDispatcher.addCallback {
}
}
}