文档返回空值(Android)

时间:2019-11-20 01:38:36

标签: java android jsoup

我正在尝试登录wikipedia.org的标题。 connect(URL).get()始终返回null。我收到“在runtime_flags中设置的未知位:0x8000”错误,但代码仍在运行。该应用程序不会崩溃,并且记录该文档为空。这是我的类,它扩展了AsyncTask:

private class Content extends AsyncTask<Void, Void, Void>
    {
    String title;
    Document doc;
    String url = "http://www.wikipedia.org";

    @Override
    protected void onPreExecute()
    {

    }

    @Override
    protected Void doInBackground(Void... voids)
    {
        try
        {
            doc  = Jsoup.connect(url).get();
            title = doc.title();

        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {
        if(doc != null)
        {
            Log.d("title", "doc found");
            Log.d("title", title);
        }
        else
        {
            Log.d("title", "nada");
        }
    }

}

编辑:这是我在Logcat中得到的内容

2019-11-20 09:00:06.620 14197-14197/? I/e.findbestpric: Not late-enabling -Xcheck:jni (already on)
2019-11-20 09:00:06.681 14197-14197/? E/e.findbestpric: Unknown bits set in runtime_flags: 0x8000
2019-11-20 09:00:06.930 14197-14197/? W/e.findbestpric: Unexpected CPU variant for X86 using defaults: x86
2019-11-20 09:00:07.115 14197-14243/? D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2019-11-20 09:00:07.126 14197-14243/? W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2019-11-20 09:00:07.104 14197-14197/? W/RenderThread: type=1400 audit(0.0:117): avc: denied { write } for name="property_service" dev="tmpfs" ino=8237 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0
2019-11-20 09:00:07.136 14197-14243/? D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2019-11-20 09:00:07.137 14197-14243/? D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2019-11-20 09:00:07.139 14197-14243/? D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2019-11-20 09:00:07.251 14197-14197/? W/e.findbestpric: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2019-11-20 09:00:07.252 14197-14197/? W/e.findbestpric: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2019-11-20 09:00:07.560 14197-14245/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-11-20 09:00:07.563 14197-14245/? W/System.err: java.io.IOException: Cleartext HTTP traffic to www.wikipedia.org not permitted
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:730)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:705)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:295)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:284)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at com.example.findbestprice.MainActivity$Content.doInBackground(MainActivity.java:105)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at com.example.findbestprice.MainActivity$Content.doInBackground(MainActivity.java:88)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:378)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-11-20 09:00:07.564 14197-14245/? W/System.err:     at java.lang.Thread.run(Thread.java:919)
2019-11-20 09:00:07.586 14197-14241/? D/HostConnection: HostConnection::get() New Host Connection established 0xd20d3e60, tid 14241
2019-11-20 09:00:07.589 14197-14241/? D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 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_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2019-11-20 09:00:07.591 14197-14241/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-11-20 09:00:07.592 14197-14241/? D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2019-11-20 09:00:07.592 14197-14241/? D/EGL_emulation: eglCreateContext: 0xdd743c80: maj 2 min 0 rcv 2
2019-11-20 09:00:07.626 14197-14241/? D/EGL_emulation: eglMakeCurrent: 0xdd743c80: ver 2 0 (tinfo 0xd212b280)
2019-11-20 09:00:07.661 14197-14241/? W/Gralloc3: mapper 3.x is not supported
2019-11-20 09:00:07.663 14197-14241/? D/HostConnection: createUnique: call
2019-11-20 09:00:07.670 14197-14241/? D/HostConnection: HostConnection::get() New Host Connection established 0xd20d58f0, tid 14241
2019-11-20 09:00:07.671 14197-14241/? D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 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_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2019-11-20 09:00:07.671 14197-14241/? D/eglCodecCommon: allocate: Ask for block of size 0x1000
2019-11-20 09:00:07.673 14197-14241/? D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ff803000 size 0x2000
2019-11-20 09:00:07.717 14197-14241/? D/EGL_emulation: eglMakeCurrent: 0xdd743c80: ver 2 0 (tinfo 0xd212b280)
2019-11-20 09:00:07.722 14197-14241/? D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2019-11-20 09:00:07.818 14197-14197/? D/title: nada

1 个答案:

答案 0 :(得分:0)

您的错误是

java.io.IOException: Cleartext HTTP traffic to www.wikipedia.org not permitted

默认情况下,Android删除了对明文流量的支持,因此要启用它,您可以尝试在android清单中添加对明文流量的支持

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

有关更多信息(如果上述操作无效),这应该可以解决您的问题:Android 8: Cleartext HTTP traffic not permitted

相关问题