java socket失败:EPERM(不允许操作)在emulator上不工作,但在插入去毛刺设备时工作正常
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");
}
}
当我插入手机并在上面运行应用程序时,它可以完美地工作。但它只适用于安卓 studio上的虚拟机模拟器。该程序只是从API中获取数据。根据错误声明,HTTP连接失败。我已在清单中启用了所需的权限。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 L安卓/view/View;->computeFitSystemWindows(L安卓/graphics/Rect;L安卓/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 L安卓/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, 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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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.安卓.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 安卓.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 安卓.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
共 (0) 个答案