我一直想知道,在没有java.net.URL的情况下,网络如何真正在android 9.0上工作(因为我的应用在android Pie崩溃时无法找到类java.net.url)。浏览网络时,我发现一些帖子说我应该使用URI,但是无论我在哪里查看,它总是使用.toURL()
。
我可以得到一些不使用java.net.url来下载图像和获取输入流的示例吗?
谢谢!
编辑:这是我不允许的堆栈跟踪:
12/17 17:24:37: Launching app
$ adb shell am start -n "xyz.mglolenstine.nekoslife/xyz.mglolenstine.nekoslife.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 14213 on device xiaomi-pocophone_f1-8e0cf863
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/stine.nekoslif: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10172(xyz.mglolenstine.nekoslife) identical 10 lines
W/stine.nekoslif: JIT profile information will not be recorded: profile file does not exits.
I/Perf: Connecting to perf service.
I/InstantRun: starting instant run server: is main process
W/stine.nekoslif: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/stine.nekoslif: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
W/stine.nekoslif: Accessing hidden field Ljava/net/URL;->host:Ljava/lang/String; (dark greylist, reflection)
W/stine.nekoslif: Accessing hidden field Ljava/net/URL;->host:Ljava/lang/String; (dark greylist, reflection)
W/stine.nekoslif: Accessing hidden field Ljava/net/URL;->host:Ljava/lang/String; (dark greylist, reflection)
W/stine.nekoslif: Accessing hidden field Ljava/net/URL;->host:Ljava/lang/String; (dark greylist, reflection)
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-3
Process: xyz.mglolenstine.nekoslife, PID: 14213
java.lang.Error: 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.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
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:198)
at khttp.requests.GenericRequest.makeRoute(GenericRequest.kt:208)
at khttp.requests.GenericRequest.<init>(GenericRequest.kt:132)
at khttp.KHttp.request(KHttp.kt:60)
at khttp.KHttp.get(KHttp.kt:30)
at khttp.KHttp.get$default(KHttp.kt:29)
at xyz.mglolenstine.nekoslife.MainActivity$addImage$1.run(MainActivity.kt:426)
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)
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-4
Process: xyz.mglolenstine.nekoslife, PID: 14213
java.lang.Error: 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.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
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:198)
at khttp.requests.GenericRequest.makeRoute(GenericRequest.kt:208)
at khttp.requests.GenericRequest.<init>(GenericRequest.kt:132)
at khttp.KHttp.request(KHttp.kt:60)
at khttp.KHttp.get(KHttp.kt:30)
at khttp.KHttp.get$default(KHttp.kt:29)
at xyz.mglolenstine.nekoslife.MainActivity$addImage$1.run(MainActivity.kt:426)
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)
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
Process: xyz.mglolenstine.nekoslife, PID: 14213
java.lang.Error: 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.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
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:198)
at khttp.requests.GenericRequest.makeRoute(GenericRequest.kt:208)
at khttp.requests.GenericRequest.<init>(GenericRequest.kt:132)
at khttp.KHttp.request(KHttp.kt:60)
at khttp.KHttp.get(KHttp.kt:30)
at khttp.KHttp.get$default(KHttp.kt:29)
at xyz.mglolenstine.nekoslife.MainActivity$addImage$1.run(MainActivity.kt:426)
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)
E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-2
Process: xyz.mglolenstine.nekoslife, PID: 14213
java.lang.Error: 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.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
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:198)
at khttp.requests.GenericRequest.makeRoute(GenericRequest.kt:208)
at khttp.requests.GenericRequest.<init>(GenericRequest.kt:132)
at khttp.KHttp.request(KHttp.kt:60)
at khttp.KHttp.get(KHttp.kt:30)
at khttp.KHttp.get$default(KHttp.kt:29)
at xyz.mglolenstine.nekoslife.MainActivity$addImage$1.run(MainActivity.kt:426)
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)
I/Process: Sending signal. PID: 14213 SIG: 9
I/Process: Sending signal. PID: 14213 SIG: 9
I/Process: Sending signal. PID: 14213 SIG: 9
Application terminated.
再一次,这只会在android 9.0(API 28)中发生,但在较低版本中会正常工作
答案 0 :(得分:0)
我是盲人,显然Java的较新版本发生了变化,该变化在android 9.0中使用,而在android中,khttp试图在java.net.URL中没有名为“ host”的字段访问。
感谢@emandt,我弄清楚了这一点,并做了自己的功能,可以完全相同而无需反射。
编辑: 一个很好的例子,它的工作方式与khttp非常相似,但仅用于get,它是用Kotlin编写的。如果您愿意,我也可以发布Java版本。
fun get(url: String): String {
try {
val connection = URI(url).toURL().openConnection() as HttpURLConnection
connection.connect()
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
return text
} catch (e: IOException) {
Log.e("Error: ", "" + e)
}
return ""
}
fun getImage(url: String): InputStream? {
try {
val connection = URI(url).toURL().openConnection() as HttpURLConnection
connection.connect()
return BufferedInputStream(connection.inputStream)
}catch(e: FileNotFoundException){
e.printStackTrace()
}
return null
}