こんにちはみんなAndroidアプリを取得していますが、起動するとコンソールにエラーが表示されます。データグラムソケットを使用して接続を作成し、2つのクラスを使用しています。 MainActivity
(アプリのメインアクティビティ)およびUdpClientServer
で接続を作成します。
ここでコード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;
}
ここで、コード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 {
}
}
}
そして、ここ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
私のAndroidManifest.xmlに文字列を追加しました
<uses-permission Android:name="Android.permission.INTERNET"/>
しかし、それは動作しません
手伝って頂けますか?ありがとう
以下を追加してみてください。
<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" />
次の許可を追加してみてください。<uses-permission Android:name="Android.permission.ACCESS_NETWORK_STATE" />