无法使用Volley(Andoid、Kotlin)连接到http站点(https工作正常)

1wnzp6jl  于 2023-01-09  发布在  Kotlin
关注(0)|答案(1)|浏览(142)

这是我第一次涉足andoroid开发,为此我决定做一个小项目来进入它。
My settup: I want to control an led strip with a Raspberry Pi Pico W with my own android app. The Pico hosts a Webpage that I can access with its local IP adress. To change the LED's I can go to, for eg:192.168.0.201/setrgb/255/000/000 this would make the LED strip Red. (This works fine in a browser)
Using this article I set up a simple string request with volley. Using the example "https://www.google.com" works just fine and I get the html code of google.com as a string. However whenever I try to access my Pico it prints: "that didn't work" (see the code below). It also doesn't work with "http://www.columbia.edu/~fdc/sample.html" although I don't really know why.
这是否与google使用https而另一个使用http的事实有关?

println("start")
        val queue = Volley.newRequestQueue(this)
        val url = "http://192.168.0.201/setrgb/000/000/000"

        val stringRequest = StringRequest(Request.Method.GET, url,
            { response ->
                println((url))
                println("response:")
                println(response)
                println("end of response")
            },
            { println("that didn't work")
            println(url)})

// Add the request to the RequestQueue.
        queue.add(stringRequest)

以下是输出:

System.out                           I  start
Volley                               V  [61] CacheDispatcher.run: start new dispatcher
System.out                           I  damn
Volley                               D  [61] WaitingRequestManager.maybeAddToWaitingRequests: new request, sending to network http://192.168.0.201/setrgb/000/000/000
HostConnection                       D  createUnique: call
HostConnection                       D  HostConnection::get() New Host Connection established 0x7cfd607d6010, tid 32234
HostConnection                       D  HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma ANDROID_EMU_hwc_multi_configs GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_
OpenGLRenderer                       W  Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
OpenGLRenderer                       W  Failed to initialize 101010-2 format, error = EGL_SUCCESS
EGL_emulation                        D  eglCreateContext: 0x7cfd607d8290: maj 2 min 0 rcv 2
EGL_emulation                        D  eglMakeCurrent: 0x7cfd607d8290: ver 2 0 (tinfo 0x7cff77380080) (first time)
Gralloc4                             I  mapper 4.x is not supported
HostConnection                       D  createUnique: call
HostConnection                       D  HostConnection::get() New Host Connection established 0x7cfd607d7b10, tid 32234
goldfish-address-space               D  allocate: Ask for block of size 0x100
goldfish-address-space               D  allocate: ioctl allocate returned offset 0x3ebffe000 size 0x2000
Gralloc4                             W  allocator 4.x is not supported
HostConnection                       D  HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma ANDROID_EMU_hwc_multi_configs GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_
Parcel                               W  Expecting binder but got null!
System.out                           I  that didn't work
System.out                           I  http://192.168.0.201/setrgb/000/000/000
Volley                               D  [2] MarkerLog.finish: (298  ms) [ ] http://192.168.0.201/setrgb/000/000/000 0x85adfa16 NORMAL 1
Volley                               D  [2] MarkerLog.finish: (+0   ) [ 2] add-to-queue
Volley                               D  [2] MarkerLog.finish: (+1   ) [61] cache-queue-take
Volley                               D  [2] MarkerLog.finish: (+0   ) [61] cache-miss
Volley                               D  [2] MarkerLog.finish: (+0   ) [62] network-queue-take
Volley                               D  [2] MarkerLog.finish: (+7   ) [62] post-error
Volley                               D  [2] MarkerLog.finish: (+290 ) [ 2] done

似乎有某种"后错误",但我真的不知道为什么,我真的不能找到任何东西,虽然谷歌。
The Following is the output if I use a working website like google.com:

[2] MarkerLog.finish: (+0   ) [ 2] add-to-queue
[2] MarkerLog.finish: (+2   ) [76] cache-queue-take
[2] MarkerLog.finish: (+0   ) [76] cache-miss
[2] MarkerLog.finish: (+1   ) [77] network-queue-take
[2] MarkerLog.finish: (+340 ) [77] network-http-complete
[2] MarkerLog.finish: (+5   ) [77] network-parse-complete
[2] MarkerLog.finish: (+3   ) [77] network-cache-written
[2] MarkerLog.finish: (+0   ) [77] post-response
[2] MarkerLog.finish: (+338 ) [ 2] done

有人知道如何解决这个问题吗?或者你有其他的想法如何实现我想要的?

vmdwslir

vmdwslir1#

最后我自己弄明白了。问题与我试图访问的网站使用http而不是https有关:
为了给予访问http网站,我在manifest.xml文件中添加了以下行:

android:usesCleartextTraffic="true"

它在<application之后。

相关问题