Android 错误消息“java.net.SocketException:套接字失败:EACCES(权限被拒绝)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11273197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:38:20  来源:igfitidea点击:

Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'

androidjson

提问by Sniver

I get the error message

我收到错误消息

java.net.SocketException: socket failed: EACCES (Permission denied)

java.net.SocketException:套接字失败:EACCES(权限被拒绝)

when I try to apply the code below. This is the function I call and gives me this exception.

当我尝试应用下面的代码时。这是我调用的函数,并给了我这个例外。

public void run() {
    // TODO Auto-generated method stub
    URL myurl = null;

    try {
        myurl = new URL("http://10.0.2.2/list.JSON");
    }
    catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        URLConnection myconn = myurl.openConnection();
        InputStream in = new BufferedInputStream(myconn.getInputStream());
        InputStreamReader reader = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(reader);
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line=br.readLine()) != null)
        {
            sb.append(line);
            //Toast.makeText(getApplicationContext(), "I enter here", Toast.LENGTH_LONG).show();
        }
        jsoncode = sb.toString();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    threading = true;
    super.run();
}

LogCat:

日志猫

06-30 11:33:21.457: W/System.err(619): java.net.SocketException: socket failed: EACCES (Permission denied)
06-30 11:33:21.467: W/System.err(619):     at libcore.io.IoBridge.socket(IoBridge.java:573)
06-30 11:33:21.467: W/System.err(619):     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
06-30 11:33:21.479: W/System.err(619):     at java.net.Socket.checkOpenAndCreate(Socket.java:663)
06-30 11:33:21.479: W/System.err(619):     at java.net.Socket.connect(Socket.java:807)
06-30 11:33:21.479: W/System.err(619):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
06-30 11:33:21.479: W/System.err(619):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-30 11:33:21.479: W/System.err(619):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
06-30 11:33:21.479: W/System.err(619):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
06-30 11:33:21.479: W/System.err(619):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-30 11:33:21.487: W/System.err(619):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-30 11:33:21.487: W/System.err(619):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-30 11:33:21.497: W/System.err(619):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-30 11:33:21.497: W/System.err(619):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-30 11:33:21.497: W/System.err(619):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-30 11:33:21.497: W/System.err(619):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-30 11:33:21.497: W/System.err(619):     at com.apk.PItestActivity$connection.run(PItestActivity.java:190)
06-30 11:33:21.507: W/System.err(619): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
06-30 11:33:21.517: W/System.err(619):     at libcore.io.Posix.socket(Native Method)
06-30 11:33:21.517: W/System.err(619):     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:169)
06-30 11:33:21.527: W/System.err(619):     at libcore.io.IoBridge.socket(IoBridge.java:558)
06-30 11:33:21.527: W/System.err(619):     ... 15 more

And this is my manifest file:

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.apk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <permission android:name="android.permission.INTERNET"></permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PItestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="addfriend"></activity>
        <activity android:name="updateDetails"></activity>
        <activity android:name="Details"></activity>
        <activity android:name="updateimage"></activity>
    </application>
</manifest>

How can I fix this problem?

我该如何解决这个问题?

回答by user370305

Try with,

试试看,

<uses-permission android:name="android.permission.INTERNET"/>

instead of,

代替,

<permission  android:name="android.permission.INTERNET"></permission>

回答by NagarjunaReddy

Try this:

尝试这个:

<uses-permission android:name="android.permission.INTERNET"/>

And your activity namesmust be like this with capital letters:

activity names必须是这样的capital letters

<activity android:name=".Addfriend"/>
    <activity android:name=".UpdateDetails"/>
    <activity android:name=".Details"/>
    <activity android:name=".Updateimage"/>

回答by Neeraj Singh

try it.

尝试一下。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

回答by Irshu

Try moving <uses-permission>outside the <application>tag.

尝试移动<uses-permission><application>标签。

回答by Gibolt

Uninstalling the appon device and then reinstallingfixed it for me.

卸载设备上的应用程序然后重新安装为我修复了它。

Tried all the other options, nothing. Finally found this post. This is after adding permission (below) and cleaning build.

尝试了所有其他选项,什么都没有。终于找到这个帖子了。这是在添加权限(如下)和清理构建之后。

<uses-permission android:name="android.permission.INTERNET"/>

回答by Nikimo Jam

It can be fixed by placing the lines in the android manifest file:

可以通过在 android 清单文件中放置以下行来修复它:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

enter image description here

在此处输入图片说明

回答by Jerin Raj

If you are using an emulator for testing then you must use <uses-permission android:name="android.permission.INTERNET" />only and ignore <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />.It's work for me.

如果您使用模拟器进行测试,那么您必须<uses-permission android:name="android.permission.INTERNET" />只使用并忽略 。<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />它对我有用。

回答by David A

You may need to do AndroidStudio - Build - Clean

你可能需要做 AndroidStudio - Build - Clean

If you updated manifest through the filesystem or Git it won't pick up the changes.

如果您通过文件系统或 Git 更新清单,它不会获取更改。

回答by CoderDream

Add those source code to your Java file as below:

将这些源代码添加到您的 Java 文件中,如下所示:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());