套接字失败:EPERM不允许操作在模拟器上不起作用,但在插入调试设备时运行良好

问题描述

public class MainActivity extends AppCompatActivity {
 public class apiGetter extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... strings) {
        String result = "";
        try {
            URL url=new URL(strings[0]);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            InputStream in=conn.getInputStream();
            InputStreamReader reader=new InputStreamReader(in);
            int data=reader.read();
            while(data!=-1){
                char current=(char)data;
                result+=current;
                data=reader.read();
            }
            return result;
        } catch (MalformedURLException e) {
            e.printstacktrace();
            return"Failed1";
        } catch (IOException e) {
            e.printstacktrace();
            return"Failed2";

        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.i("mine",s);
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String result=null;
    apiGetter task=new apiGetter();
    task.execute("https://evilinsult.com/generate_insult.PHP?lang=en&type=json");

}
}

当我插入手机并在其上运行应用程序时,它运行良好。但它只对我在 android studio 上的 VM 模拟器不起作用。该程序只是从 API 中获取数据。根据错误声明,HTTP 连接失败。我也在 manifest.xml 中启用了所需的权限。否则当我的手机插入时它不会工作 我的错误输出是:

enter code here2021-01-10 21:49:59.712 29356-29356/? I/m.example.json: Not late-enabling -Xcheck:jni 
(already on)
2021-01-10 21:49:59.754 29356-29356/? E/m.example.json: UnkNown bits set in runtime_flags: 0x8000
2021-01-10 21:49:59.755 29356-29356/? W/m.example.json: Unexpected cpu variant for X86 using         defaults: x86
2021-01-10 21:50:00.262 29356-29392/com.example.json1 D/libEGL: Emulator has host GPU support,qemu.gles is set to 1.
2021-01-10 21:50:00.263 29356-29392/com.example.json1 W/libc: Unable to set property "qemu.gles" to     "1": connection Failed; errno=13 (Permission denied)
2021-01-10 21:50:00.248 29356-29356/com.example.json1 W/RenderThread: type=1400 audit(0.0:258): avc: denied { write } for name="property_service" dev="tmpfs" ino=7023     scontext=u:r:untrusted_app:s0:c139,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.json1
2021-01-10 21:50:00.300 29356-29392/com.example.json1 D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2021-01-10 21:50:00.321 29356-29392/com.example.json1 D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2021-01-10 21:50:00.326 29356-29392/com.example.json1 D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2021-01-10 21:50:00.680 29356-29356/com.example.json1 W/m.example.json: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist,reflection,allowed)
2021-01-10 21:50:00.681 29356-29356/com.example.json1 W/m.example.json: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist,allowed)
2021-01-10 21:50:00.866 29356-29394/com.example.json1 D/NetworkSecurityConfig: No Network Security Config specified,using platform default
2021-01-10 21:50:00.894 29356-29394/com.example.json1 W/System.err: java.net.socketException: socket Failed: EPERM (Operation not permitted)
2021-01-10 21:50:00.894 29356-29394/com.example.json1 W/System.err:     at java.net.socket.createImpl(Socket.java:492)
2021-01-10 21:50:00.895 29356-29394/com.example.json1 W/System.err:     at java.net.socket.getImpl(Socket.java:552)
2021-01-10 21:50:00.896 29356-29394/com.example.json1 W/System.err:     at java.net.socket.setSoTimeout(Socket.java:1180)
2021-01-10 21:50:00.898 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
2021-01-10 21:50:00.899 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
2021-01-10 21:50:00.900 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
2021-01-10 21:50:00.900 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
2021-01-10 21:50:00.900 29356-29394/com.example.json1 W/System.err:     at     com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
2021-01-10 21:50:00.900 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
2021-01-10 21:50:00.901 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
2021-01-10 21:50:00.901 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
2021-01-10 21:50:00.902 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
2021-01-10 21:50:00.902 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
2021-01-10 21:50:00.903 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
2021-01-10 21:50:00.903 29356-29394/com.example.json1 W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at com.example.json1.MainActivity$apiGetter.doInBackground(MainActivity.java:27)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at com.example.json1.MainActivity$apiGetter.doInBackground(MainActivity.java:19)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:378)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-01-10 21:50:00.904 29356-29394/com.example.json1 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-01-10 21:50:00.905 29356-29394/com.example.json1 W/System.err:     at java.lang.Thread.run(Thread.java:919)
2021-01-10 21:50:00.964 29356-29390/com.example.json1 D/HostConnection: HostConnection::get() New     Host Connection established 0x93e3fb40,tid 29390
2021-01-10 21:50:00.969 29356-29390/com.example.json1 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_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 
2021-01-10 21:50:00.973 29356-29390/com.example.json1 W/Openglrenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED,retrying without...
2021-01-10 21:50:00.990 29356-29390/com.example.json1 D/EGL_emulation: eglCreateContext: 0xa00879a0: maj 2 min 0 rcv 2
2021-01-10 21:50:00.995 29356-29390/com.example.json1 D/EGL_emulation: eglMakeCurrent: 0xa00879a0: ver 2 0 (tinfo 0xa008fcb0)
2021-01-10 21:50:01.038 29356-29390/com.example.json1 W/Gralloc3: mapper 3.x is not supported
2021-01-10 21:50:01.042 29356-29390/com.example.json1 D/HostConnection: createUnique: call
2021-01-10 21:50:01.042 29356-29390/com.example.json1 D/HostConnection: HostConnection::get() New Host Connection established 0x93e41490,tid 29390
2021-01-10 21:50:01.046 29356-29390/com.example.json1 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_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 
2021-01-10 21:50:01.046 29356-29390/com.example.json1 D/eglCodecCommon: allocate: Ask for block of size 0x1000
2021-01-10 21:50:01.047 29356-29390/com.example.json1 D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff4000 size 0x2000
021-01-10 21:50:01.145 29356-29390/com.example.json1 D/EGL_emulation: eglMakeCurrent: 0xa00879a0: ver 2 0 (tinfo 0xa008fcb0)
2021-01-10 21:50:01.305 29356-29356/com.example.json1 I/mine: Failed2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)