Когда я пытаюсь запросить API с khttp
У меня возникает эта проблема в эмуляторе на Android Studio с SDK 28 (Android Pie 9.0)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.corentin.myapplication, PID: 3168
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NoSuchFieldException: No field host in class Ljava/net/URL; (declaration of 'java.net.URL' appears in /system/framework/core-oj.jar)
at java.lang.Class.getDeclaredField(Native Method)
at khttp.requests.GenericRequest.toIDN(GenericRequest.kt:193)
at khttp.requests.GenericRequest.makeRoute(GenericRequest.kt:198)
at khttp.requests.GenericRequest.<init>(GenericRequest.kt:128)
at khttp.KHttp.request(KHttp.kt:58)
at khttp.KHttp.get(KHttp.kt:28)
at khttp.KHttp.get$default(KHttp.kt:27)
at com.example.corentin.myapplication.InfoUser.run(InfoUser.kt:29)
at com.example.corentin.myapplication.InfoUser$onCreate$1.invoke(InfoUser.kt:23)
at com.example.corentin.myapplication.InfoUser$onCreate$1.invoke(InfoUser.kt:16)
at com.example.corentin.myapplication.doAsync.doInBackground(InfoUser.kt:11)
at com.example.corentin.myapplication.doAsync.doInBackground(InfoUser.kt:9)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
И вот откуда возникает ошибка:
class doAsync(val handler: () -> Unit) : AsyncTask<Void, Void, Void>() {
override fun doInBackground(vararg params: Void?): Void? {
handler()
return null
}
}
class InfoUser : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_info_user)
doAsync {
run("https://api.imgur.com/3/account/xcoco55?client_id=CLIENT ID")
}.execute()
}
fun run(url: String) {
val response = khttp.get(url)
println("Coucou")
if (response.statusCode == 200) {
runOnUiThread {textView.text = response.text}
}
}
}
Когда я удаляю строку val response = khttp.get(url)
или когда я запускаю приложение на своем мобильном телефоне (Android Oreo 8.1), все работает нормально.
Вы думаете, что ошибка исходит от Android 9.0 или от эмулятора?