194

I get the error message

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

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sniver
  • 1,949
  • 2
  • 11
  • 6

9 Answers9

378

Try with,

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

instead of,

<permission  android:name="android.permission.INTERNET"></permission>
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 1
    +1. Also, albeit this has nothing to do with the actual problem, but: don't forget to install the *right* apk after you make this change... I've tricked myself by keeping another copy of the .apk in a distinct directory and forgot to overwrite it with the new one before installing from there. – n611x007 Sep 11 '13 at 15:53
  • Worked for me as of nov, 2013. Using eclipse, you can edit the AndroidManifest.xml with the fancy editor that has a Permissions tab. Add a Uses Permission and pick this permission from the dropdown menu. – jjohn Nov 08 '13 at 20:33
  • 4
    important - the uses-permission tag needs to be before the application tag in the manifest file. thats what fixed it for me at least.. – Or Gal Oct 14 '14 at 20:40
41

Try this:

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

And your activity namesmust be like this with capital letters:

<activity android:name=".Addfriend"/>
    <activity android:name=".UpdateDetails"/>
    <activity android:name=".Details"/>
    <activity android:name=".Updateimage"/>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • 4
    - If you have additional information in answer, then only post if you have same as already answered then support that answer. – user370305 Jun 30 '12 at 10:06
  • 1
    @user370305 excuse me, do you mean that he should only post the additional information? not being native, I cannot figure out what you meant but I'd like to know about meta stuff. – n611x007 Sep 11 '13 at 14:05
30

Try adding the following:

<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" />
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Cyd
  • 1,245
  • 1
  • 14
  • 17
20

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

Irshu
  • 8,248
  • 8
  • 53
  • 65
11

Uninstalling the app on device and then reinstalling fixed 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"/>
Gibolt
  • 42,564
  • 15
  • 187
  • 127
2

try to add the following permission :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Sadik anass
  • 282
  • 4
  • 9
1

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.

Jerin Raj
  • 383
  • 2
  • 15
0

Add those source code to your Java file as below:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
CoderDream
  • 41
  • 3
  • 5
    This is not the answer to this question which is about missing permission in manifest. And this is a very bad answer to the problem it is an answer to, i.e. `NetworkOnMainThreadException`. – laalto Dec 18 '13 at 07:09
0

You may need to do AndroidStudio - Build - Clean

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

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39