Android 9.0 Java.net.URL

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


我不知道你为什么遇到麻烦,因为java.net.url是Android 9的一部分。它没有被弃用。请发布您的代码和实际堆栈跟踪。 - Gabe Sechan
我正在使用khttp,将在一个小时内发布堆栈跟踪。 - MG lolenstine
我已经更新了我的问题并附上了堆栈跟踪。 - MG lolenstine
错误似乎是自我解释的:“java.lang.NoSuchFieldException: No field host in class Ljava/net/URL”。看起来变量“host”在Android 9的“java.net.URL”类中不再存在。这是因为“kHTTP”似乎使用反射来获取隐藏变量。 - emandt
嗯,有点奇怪...我会去看看的,谢谢! - MG lolenstine
公正的观点,你是正确的,而我是盲目的。感谢您的帮助,我不得不编写自己的函数来处理GET请求和图像下载,现在它可以工作了!谢谢。 - MG lolenstine
1个回答

0

我之前瞎了,显然Java的新版本在Android 9.0中有所改变,'java.net.URL'中没有名为“host”的字段,而khttp正试图访问它。

感谢@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
    }

谢谢!我最终fork了kHttp库并修补了依赖于修改API请求的函数调用。如果您感兴趣,可以在github.com/karn/khttp上查看它。 - Karn
非常感谢,我也已经fork了它,但从未有机会修补它。你应该向原始仓库发送一个pull request。 :) - MG lolenstine
1
不幸的是,kHttp已经不再维护了。我已经开始在我的一侧维护代码的过程。 - Karn
1
嗯...猜猜谁的GitHub仓库正在被收藏和书签化。感谢你做了这一切! - MG lolenstine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接