我有一个包含编辑文本的客户端类,输入的文本将通过LAN发送到特定主机,其IP定义为按下按钮。但它不起作用而且停止了。请帮我。 我的客户端类是:
public class MainActivity extends Activity {
EditText et1;
Button bt1;
InetAddress IPAddress;
DatagramPacket dp;
String message;
int SERVERPORT = 11111;
String SERVER_IP = "192.168.1.101";
DatagramSocket clientSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.EditText01);
bt1=(Button)findViewById(R.id.myButton);
message=et1.getText().toString();
try {
clientSocket = new DatagramSocket();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
IPAddress = InetAddress.getByName(SERVER_IP);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] sendData = message.getBytes();
dp = new DatagramPacket(sendData, sendData.length, IPAddress, SERVERPORT);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
clientSocket.send(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
log cat is:
08-27 07:04:52.967: D/gralloc_goldfish(531): Emulator without GPU emulation detected.
08-27 07:05:03.018: D/AndroidRuntime(531): Shutting down VM
08-27 07:05:03.018: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-27 07:05:03.048: E/AndroidRuntime(531): FATAL EXCEPTION: main
08-27 07:05:03.048: E/AndroidRuntime(531): android.os.NetworkOnMainThreadException
08-27 07:05:03.048: E/AndroidRuntime(531): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
08-27 07:05:03.048: E/AndroidRuntime(531): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:163)
08-27 07:05:03.048: E/AndroidRuntime(531): at libcore.io.IoBridge.sendto(IoBridge.java:463)
08-27 07:05:03.048: E/AndroidRuntime(531): at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
08-27 07:05:03.048: E/AndroidRuntime(531): at java.net.DatagramSocket.send(DatagramSocket.java:287)
08-27 07:05:03.048: E/AndroidRuntime(531): at com.example.alastclient.MainActivity$1.onClick(MainActivity.java:73)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.view.View.performClick(View.java:3480)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.view.View$PerformClick.run(View.java:13983)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.os.Handler.handleCallback(Handler.java:605)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.os.Looper.loop(Looper.java:137)
08-27 07:05:03.048: E/AndroidRuntime(531): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-27 07:05:03.048: E/AndroidRuntime(531): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:05:03.048: E/AndroidRuntime(531): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 07:05:03.048: E/AndroidRuntime(531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-27 07:05:03.048: E/AndroidRuntime(531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-27 07:05:03.048: E/AndroidRuntime(531): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的客户端应该包含用于在网络上发送数据包的新线程,在现有类中,您使用主线程以通过网络发送udp数据包,我认为这是不可行的,您必须创建一个按下按钮时的新线程,请参阅runnable和thread的android文档。