当前位置: 首页 > 知识库问答 >
问题:

JAVA网SocketException:套接字失败:EACCES(权限被拒绝)

左丘弘致
2023-03-14

我已经找到了所有的解决方案并应用到我的项目中。但它们不起作用。我被拒绝了这个许可。我已经把所有可能的许可都放进了清单文件。请告诉我我的项目出了什么问题。

public static void getGetResponse() {
        InputStream inputStream = null;
        try {
            URL url = new URL("http://120.26.89.113:8080/common/qiniuToken");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");          
            String userPassword = "wehelper:***********";
            byte[] encodedBytes = Base64.encode(userPassword.getBytes(), 0);
            connection.setRequestProperty("Authorization", "Basic " + encodedBytes);
            connection.connect();
            int resCode = connection.getResponseCode();
            if (resCode == 200) {
                inputStream = connection.getInputStream();
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder out = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                out.append(line);
            }
            System.out.println(out.toString());//Prints the string content read from input stream
            Log.d("myApp",out.toString());
            reader.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我的manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">
    <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" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Logcat:

W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:619)
W/System.err:     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
W/System.err:     at java.net.Socket.checkOpenAndCreate(Socket.java:689)
W/System.err:     at java.net.Socket.setSoTimeout(Socket.java:543)
W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:433)
W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:105)
W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:1331)
W/System.err:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1410)
W/System.err:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:466)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:468)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
W/System.err:     at com.donghuaxue.spoonhunt.HttpClient.getGetResponse(HttpClient.java:44)
W/System.err:     at com.donghuaxue.spoonhunt.MainActivity.onCreate(MainActivity.java:42)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6876)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
W/System.err:     at android.app.ActivityThread.access$1100(ActivityThread.java:222)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:158)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Posix.socket(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:604)
W/System.err:   ... 27 more

共有1个答案

贺亦
2023-03-14

您的<代码>

<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" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
 类似资料: