SocketException:权限被拒绝?

时间:2011-08-19 13:31:20

标签: android url inputstream permission-denied socketexception

我的LogCat读取:

08-19 09:29:01.964: WARN/System.err(311): java.net.SocketException: Permission denied
08-19 09:29:02.204: WARN/System.err(311):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
08-19 09:29:02.214: WARN/System.err(311):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
08-19 09:29:02.214: WARN/System.err(311):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
08-19 09:29:02.224: WARN/System.err(311):     at java.net.Socket.checkClosedAndCreate(Socket.java:873)
08-19 09:29:02.224: WARN/System.err(311):     at java.net.Socket.connect(Socket.java:1020)
08-19 09:29:02.224: WARN/System.err(311):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
08-19 09:29:02.224: WARN/System.err(311):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
08-19 09:29:02.224: WARN/System.err(311):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
08-19 09:29:02.224: WARN/System.err(311):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
08-19 09:29:02.234: WARN/System.err(311):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
08-19 09:29:02.234: WARN/System.err(311):     at java.net.URL.openStream(URL.java:653)
08-19 09:29:02.244: WARN/System.err(311):     at around.lowell.Main.onCreate(Main.java:57)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-19 09:29:02.244: WARN/System.err(311):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 09:29:02.244: WARN/System.err(311):     at android.os.Looper.loop(Looper.java:123)
08-19 09:29:02.244: WARN/System.err(311):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-19 09:29:02.254: WARN/System.err(311):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 09:29:02.254: WARN/System.err(311):     at java.lang.reflect.Method.invoke(Method.java:521)
08-19 09:29:02.254: WARN/System.err(311):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-19 09:29:02.264: WARN/System.err(311):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-19 09:29:02.264: WARN/System.err(311):     at dalvik.system.NativeStart.main(Native Method)

我最近实现的代码是在我的Android应用中生成此错误:

        FileOutputStream fOut = null;
        try {
            fOut = this.openFileOutput("employeeInformation.xml", MODE_PRIVATE);
            try {
                InputStream is = new URL("http://totheriver.com/learn/xml/code/employees.xml").openStream();
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                fOut.write(buffer);
             } catch(Exception e) {
                 e.printStackTrace();
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

有谁知道问题是什么?

2 个答案:

答案 0 :(得分:128)

向您的清单添加Internet权限:

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

答案 1 :(得分:3)

您需要转到Android应用程序项目中的Manifest文件,然后在其他权限旁边的application部分上添加:android.permission.INTERNET

你的清单应该是这样的:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
....
<uses-permission android:name="android.permission.INTERNET" />
...
<application
android:allowBackup="true"
....

请记住,值(android.permission.INTERNET)区分大小写。

希望这可以节省你一些时间。