网络代码有效,问题在于您没有在等待响应,即该
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 UIrunonUiThread(new Runnable() { @Override public void run() { // this is executed on the main (UI) thread TextOne.setText(Response); }});dsocket.close();通常,必须在后台线程上工作并在主线程上处理结果。Android提供了一些工具,例如Handler或AsyncTask



