Je reçois le message d'erreur
Java.net.SocketException: échec de la socket: EACCES (autorisation refusée)
quand j'essaie d'appliquer le code ci-dessous. C'est la fonction que j'appelle et qui me donne cette 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
Et voici mon fichier manifeste:
<?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>
Comment puis-je résoudre ce problème?
Essayez avec,
<uses-permission Android:name="Android.permission.INTERNET"/>
au lieu de,
<permission Android:name="Android.permission.INTERNET"></permission>
Essaye ça:
<uses-permission Android:name="Android.permission.INTERNET"/>
Et votre activity names
doit être comme ça avec capital letters
:
<activity Android:name=".Addfriend"/>
<activity Android:name=".UpdateDetails"/>
<activity Android:name=".Details"/>
<activity Android:name=".Updateimage"/>
l'essayer.
<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" />
Essayez de déplacer <uses-permission>
en dehors de la balise <application>
.
Ajoutez ce code source à votre fichier Java comme ci-dessous:
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
Vous aurez peut-être besoin de faire Android Studio - Build - Clean
Si vous avez mis à jour manifeste via le système de fichiers ou Git, les modifications ne seront pas prises en compte.