0

我正在编写一个小应用程序,其中 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)

我的安卓程序是:

    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();
}
4

2 回答 2

2

android.os.NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。

在您的情况下,您正在从 MainUI 线程运行网络操作,这就是您得到此异常的原因(在 android 中从未允许这样做)。使用Android AsyncTask并将您的网络(套接字代码)放入doInBackGround()AsyncTask 中。

看SO问题android.os.NetworkOnMainThreadException

于 2012-07-02T09:45:44.597 回答
1

正如文档所说:android.os。NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

所以尝试通过使用AsyncTaskHandlerHandlerThreadrunOnUiThread来实现你的网络相关代码

于 2012-07-02T09:50:16.940 回答