Salut les gars, je reçois une application Android mais quand je la lance, je reçois une erreur dans ma console. J'utilise une prise Datagramme pour créer une connexion et j'utilise 2 classes: MainActivity
(c'est l'activité principale de l'application) et UdpClientServer
pour créer la connexion.
Voici le code MainActivity:
public class MainActivity extends Activity {
private UdpClientServer cu;
private EditText textIpScheda;
private EditText textUdpPort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIpScheda = (EditText) findViewById(R.id.textIpScheda);
textUdpPort = (EditText) findViewById(R.id.textUdpPort);
try {
cu = new UdpClientServer(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public EditText getTextIpScheda(){
return textIpScheda;
}
public void setTextIpScheda(EditText textIpScheda){
this.textIpScheda = textIpScheda;
}
public EditText getTextUdpPort() {
return textUdpPort;
}
public void setTextUdpPort(EditText textUdpPort) {
this.textUdpPort = textUdpPort;
}
Voici le code UdpClientServer:
public class UdpClientServer {
public static String sReceive;
private static DatagramSocket dSocket;
int receiveBufferSize = 1024;
int portUdp = 0;
final String PINGACMD = "AT*PINGA001";
InetAddress ipScheda;
byte[] receiveData = new byte[receiveBufferSize];
private MainActivity gui = null;
public UdpClientServer(MainActivity gui) throws SocketException, IOException {
this.gui = gui;
portUdp = Integer.parseInt(String.valueOf(gui.getTextUdpPort().getText()));
dSocket = new DatagramSocket(portUdp);
}
public void run(){
while (true) {
// svuotamento buffer
Arrays.fill(receiveData, (byte) 0);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
dSocket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
ipScheda = receivePacket.getAddress();
int port = receivePacket.getPort();
gui.getTextUdpPort().setText("" + port);
gui.getTextIpScheda().setText(ipScheda.getHostAddress());
sReceive = new String(receivePacket.getData());
this.sendCommand(PINGACMD);
}
}
public void sendCommand(String outSentence){
byte[] sendData = outSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipScheda, portUdp);
try {
dSocket.send(sendPacket);
Thread.sleep(100);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
}
}
}
Et voici le logcat:
12-29 11:43:22.291 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ Java.net.SocketException: socket failed: EACCES (Permission denied)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.Java:623)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.Java:93)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Java.net.DatagramSocket.createSocket(DatagramSocket.Java:157)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Java.net.DatagramSocket.<init>(DatagramSocket.Java:80)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.example.matteo.myfirstapp.UdpClientServer.<init>(UdpClientServer.Java:32)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.example.matteo.myfirstapp.MainActivity.onCreate(MainActivity.Java:24)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.Activity.performCreate(Activity.Java:5933)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1105)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2251)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.os.Handler.dispatchMessage(Handler.Java:102)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.os.Looper.loop(Looper.Java:135)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Android.app.ActivityThread.main(ActivityThread.Java:5221)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Java.lang.reflect.Method.invoke(Native Method)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at Java.lang.reflect.Method.invoke(Method.Java:372)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:899)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ Caused by: Android.system.ErrnoException: socket failed: EACCES (Permission denied)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.Posix.socket(Native Method)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.Java:282)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.Java:608)
12-29 11:43:22.294 28914-28914/com.example.matteo.myfirstapp W/System.err﹕ ... 18 more
Dans mon AndroidManifest.xml vient d'ajouter une chaîne
<uses-permission Android:name="Android.permission.INTERNET"/>
mais ça ne marche pas
Pouvez-vous m'aider? Merci
Essayez d'ajouter les éléments suivants:
<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 d'ajouter l'autorisation suivante:<uses-permission Android:name="Android.permission.ACCESS_NETWORK_STATE" />