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

时间:2016-07-04 14:55:30

标签: java android httpurlconnection permission-denied

我搜索了所有解决方案并应用到我的项目中。但他们没有工作。我已将此权限拒绝。我已将所有可能的权限放入清单文件中。请告诉我我的项目有什么问题。

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 个答案:

答案 0 :(得分:5)

您的<uses-permission>元素位置错误。他们需要之外的<{1}}元素,作为根<application>元素的直接子元素:

<manifest>