网络代码有效,问题在于您没有在等待响应,即该SendUdpMsg
方法无法立即返回响应,应该由后台线程推送该响应。例如 :
dsocket.receive(packet2);
Response = new String(buffer, 0, packet2.getLength());
// we received the response
// but since we are running on a background thread, we can not touch the UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// this is executed on the main (UI) thread
TextOne.setText(Response); }
});
dsocket.close();