一个奇怪的NetWorkOnMainThreadException

时间:2013-09-28 17:44:00

标签: android multithreading exception xmpp smack

据我们所知,我们无法在UI线程中执行Web连接,并尝试在Thread或AsyncTask上执行此操作,并且我选择在可运行的Thread中运行Web连接。当我单击按钮时,log会显示正确的代码,connection.connect();恰好是NetworkOnMainThreadException,但是你知道,从我下面的代码中,我已经在线程中编写了我的networkconnecton代码,所以,任何人都告诉我正确的处理这个问题的方法?

public class MainActivity extends Activity {

    private Button btn_send;
    private EditText et_content;


    private Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_send = (Button) findViewById(R.id.bt_sendms);

        et_content = (EditText) findViewById(R.id.et_inputcontent);

        btn_send.setOnClickListener(onClicListener);

    }

    public OnClickListener onClicListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            handler.post(task);
        }

    };

    private Handler handler = new Handler();

    private Runnable task = new Runnable() {



        @Override
        public void run() {
            // TODO Auto-generated method stub

            XMPPConnection.DEBUG_ENABLED = true;

         ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
                    "192.168.1.108", Integer.parseInt("5222"), "dell-PC");

            AccountManager accountManager;

            connectionConfig.setReconnectionAllowed(true);
            connectionConfig.setSendPresence(true);

            connection = new XMPPConnection(connectionConfig);

            try {

                connection.connect();
                accountManager = connection.getAccountManager(); // 
                connection.login("admin", "888889");
                ChatManager chatManager = connection.getChatManager();

                Chat newChat = chatManager.createChat("bryanwu@qq.com",
                        new MessageListener() {

                            @Override
                            public void processMessage(Chat chat,
                                    Message message) {

                            }
                        });

                try {
                    newChat.sendMessage(et_content.getText().toString());
                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                et_content.setText("");

            } catch (XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 

        }

    };

}

1 个答案:

答案 0 :(得分:0)

您在ui线程上创建了一个处理程序。您可以在runnable run方法中运行与网络相关的操作。所以你在ui线程上运行网络相关的操作。

使用ThreadAsynctask

http://developer.android.com/reference/android/os/AsyncTask.html

引自doscs

每个Handler实例都与一个线程和该线程的消息队列相关联。 当你创建一个新的Handler时,它被绑定到创建它的线程的线程/消息队列 - 从那时起,它将消息和runnables传递给该消息队列并在它们出现时执行它们消息队列。

如果您在ui post honey上运行网络相关操作,您将获得NetworkOnMainThreadException

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html