Amit Koranne
Amit Koranne

Reputation: 141

Permission denied in while making http request

i am making request to request with following and i am getting exception in android application.

URL url = new URL(strURL);
URLConnection urlConnection = url.openConnection();

try {
    HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
    httpConn.setRequestMethod("GET");
    httpConn.connect();

    if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        is = httpConn.getInputStream();

    }
} catch (Exception ex) {

    System.out.println(ex);
}

getting following exception

java.net.SocketException: Permission denied

Upvotes: 2

Views: 2773

Answers (3)

R9J
R9J

Reputation: 6715

Add this to your manifest.xml

<!-- For Getting Network permission -->
<uses-permission android:name="android.permission.INTERNET"/>

Upvotes: 2

Bigflow
Bigflow

Reputation: 3666

First, put this in the manifest:

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

If your app is going to be released on Android versions higher then 3.0, you must keep in mind that you need to make the http request in an other thread (Option 1 and 2).

Option 1 (not recommended, but it works: Add this line:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

But I do not reccomend the first option to be honest, but it does work. //===========================================================================
Option 2:

Used this tutorial to make a proper ASyncTask: http://www.elvenware.com/charlie/development/android/SimpleHttpGetThread.html

//=========================================================================== Used ASyncTask as final (option 2).

They changed the network permissions in Android 3.0+

Upvotes: 2

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> to manifest

Upvotes: 2

Related Questions