我正在编写一个小应用程序,其中android设备尝试将数据包发送到服务器(这是我自己的计算机)。
我收到一些错误,我不知道如何修复它。
这是错误:
>07-02 14:58:56.125: E/AndroidRuntime(17976): FATAL EXCEPTION: main
>07-02 14:58:56.125: E/AndroidRuntime(17976): android.os.NetworkOnMainThreadException
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.IoBridge.connectErrno(IoBridge.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.IoBridge.connect(IoBridge.java:112)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.Socket.startupSocket(Socket.java:566)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.Socket.tryAllAddresses(Socket.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at java.net.Socket.<init>(Socket.java:177)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at java.net.Socket.<init>(Socket.java:149)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.amazon.hsyal.WifiSimulatorActivity$1.onClick(WifiSimulatorActivity.java:33)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.view.View.performClick(View.java:3536)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.view.View$PerformClick.run(View.java:14130)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.Handler.handleCallback(Handler.java:605)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.Handler.dispatchMessage(Handler.java:92)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at android.os.Looper.loop(Looper.java:137)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.app.ActivityThread.main(ActivityThread.java:4495)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.lang.reflect.Method.invokeNative(Native Method)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.lang.reflect.Method.invoke(Method.java:511)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at dalvik.system.NativeStart.main(Native >Method)
我的android程序是:
public class WifiSimulatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button send_packet_button = (Button) findViewById(R.id.send_packet_button);
send_packet_button.setOnClickListener(sendPacketListener);
}
Button.OnClickListener sendPacketListener = new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Socket s = new Socket("192.168.52.138", 12345);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output.write("Sending packet 1");
s.close();
}
catch(UnknownHostException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
};
}
public static void main(String[] args){
try{
Boolean end = false;
ServerSocket ss = new ServerSocket(12345);
System.out.println("Program running");
while(!end){
//Server is waiting for client here, if needed
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
System.out.println("Tcp Example From client: "+st);
output.println("Good bye and thanks for all the fish :)");
s.close();
}
ss.close();
}
答案 0 :(得分:2)
<强> android.os.NetworkOnMainThreadException 强>
应用程序尝试执行时抛出的异常 在其主线程上进行网络操作。
这仅适用于针对Honeycomb SDK或。的应用程序 更高。允许使用早期SDK版本的应用程序 他们的主要事件循环线程上的网络,但它是很重要的 气馁。
在您的情况下您正在从MainUI线程运行网络操作,这就是您获得此异常的原因(在Android中从不允许这样做)。使用Android AsyncTask并将您的网络(套接字代码)放在AsyncTask的doInBackGround()
中。
答案 1 :(得分:1)
正如文档所说:android.os。NetworkOnMainThreadException:
应用程序尝试执行时抛出的异常 在其主线程上进行网络操作。
因此,请尝试使用AsyncTask,Handler,HandlerThread和runOnUiThread
来实施您的网络相关代码