首页 > 解决方案 > Android 9.0 Java.net.URL

问题描述

我一直在想,没有 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) 中,但在较低版本中可以完美运行

标签: javaandroidkotlin

解决方案


我是盲人,显然在 android 9.0 中使用的较新版本的 Java 发生了变化,它在 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
    }

推荐阅读