webentwicklung-frage-antwort-db.com.de

SocketException: Berechtigung verweigert?

Mein LogCat liest:

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)

Der Code, den ich kürzlich implementiert habe, für den dieser Fehler in meiner Android App generiert wird, lautet:

        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();
            }
        }

Weiß jemand was das Problem ist?

46
Mxyk

Fügen Sie Ihrem Manifest eine Internet-Berechtigung hinzu:

<uses-permission Android:name="Android.permission.INTERNET"/>
131
Cristian

Sie müssen zu Ihrer Manifest-Datei in Ihrem Android) Anwendungsprojekt gehen und dann über dem Abschnitt application neben anderen Berechtigungen Folgendes hinzufügen: Android.permission.INTERNET

Ihr Manifest sollte ungefähr so ​​aussehen:

<?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"
....

bedenken Wert (Android.permission.INTERNET) Gross- und Kleinschreibung.

Hoffe das spart dir etwas Zeit.

4