2

我提前为格式道歉。我是 android 开发的新手,我正在开发一个 XMPP 客户端,与家里的服务器交谈,通过谷歌谈话机器人执行命令....

  1. 我确实查看了stackoverflow的一些答案并搜索了一些答案
  2. 我通过 github 更新到最新的 ASMACK 库
  3. 我确实添加了使用权限
  4. 我让 ASMACK 在我的 android 上开发一个测试应用程序。

我将工作代码复制到我正在尝试制作的应用程序中并给了我错误......就像我说我是新手,10 年来没有做过 java 编程......我不知道如何听到错误堆栈

我有一个名为“设置对话框”的设置对话框。当我单击按钮时,我运行:

    protected void onStart() {
    super.onStart();
    setContentView(R.layout.settings);
    getWindow().setFlags(4, 4);
    setTitle("XMPP Settings");
    Button ok = (Button) findViewById(R.id.ok);
    ok.setOnClickListener( new View.OnClickListener()
    {
            @Override
            public void onClick(View v)
            {
                String host = HOST_ADDRESS;
                int port = HOST_PORT_NUM;
                String service = HOST_SERVICE;
                String username = "me@gmail.com";//getText(R.id.userid);
                String password = "abc123";//getText(R.id.password);

                // Create a connection
                ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
                XMPPConnection XMPPCon = new XMPPConnection(connConfig);
                try 
                {
                    XMPPCon.connect();
                    XMPPCon.login(username,password);

                } 
                catch (XMPPException ex) 
                {
                    ex.printStackTrace();
                    HomeBotClient.setConnection(null);
                }
                HomeBotClient.setConnection(XMPPCon);
                dismiss();
            }

    }
    );
}

这在主要活动中称为“setConnection”函数

public void setConnection (XMPPConnection XMPPCon) {

    this.XMPPCon = XMPPCon;
    if (XMPPCon != null) 
    {
        ChatManager chatmanager = XMPPCon.getChatManager();
        Chat newChat = chatmanager.createChat("psun03@gmail.com",new MessageListener()
        {
            public void processMessage(Chat chat, Message message) {
                try 
                {
                  Log.v(TAG, "Got:" + message.getBody());
                  chat.sendMessage(message.getBody());
                } 
                catch (XMPPException e) 
                {
                    e.printStackTrace();
                    Log.v(TAG, "Couldn't respond:" + e);
                }
                Log.v(TAG, message.toString());
              }
        });
        try 
        {
            newChat.sendMessage("OMNOMNOM");
        } 
        catch (XMPPException e) 
        {
            e.printStackTrace();
            Log.v(TAG, "couldn't send:" + e.toString());
        }

          // Accept only messages from friend@gmail.com
          PacketFilter filter 
              = new AndFilter(new PacketTypeFilter(Message.class), 
                              new FromContainsFilter("psun03@gmail.com"));

          // Collect these messages
          PacketCollector collector = XMPPCon.createPacketCollector(filter);

          while(true) {
            Packet packet = collector.nextResult();

            if (packet instanceof Message) 
            {
              Message msg = (Message) packet;
              // Process message
              Log.v(TAG, "Got message:" + msg.getBody());
            }
          }
    }
}

按下按钮时出现此错误

04-07 17:21:15.011: E/AndroidRuntime(1099): FATAL EXCEPTION: main 04-07 17:21:15.011: E/AndroidRuntime(1099): android.os.NetworkOnMainThreadException 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 04-07 17:21:15.011: E/AndroidRuntime(1099): at java.net.InetAddress.getAllByName(InetAddress.java:214) 04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:49) 04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:576) 04-07 17:21:15.011: E/AndroidRuntime(1099): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1034) 04-07 17:21:15.011: E/AndroidRuntime(1099): at com.ps.homebotclient.SettingsDialog$1.onClick(SettingsDialog.java:53) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.view.View.performClick(View.java:4204) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.view.View$PerformClick.run(View.java:17355) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Handler.handleCallback(Handler.java:725) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:92) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:137) 04-07 17:21:15.011: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-07 17:21:15.011: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method) 04-07 17:21:15.011: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:511) 04-07 17:21:15.011: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-07 17:21:15.011: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-07 17:21:15.011: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)

4

2 回答 2

2

Android 4.0 或更高版本不允许在 GUI 线程中进行网络 I/O 操作,NetworkOnMainThread如果这样做会抛出异常。为了防止这种情况,aSmack 带有AndroidConnectionConfiguration,它提供

  • AndroidConnectionConfiguration(String serviceName)
  • AndroidConnectionConfiguration(String serviceName, int timeout)
  • AndroidConnectionConfiguration(String host, int port, String name)

它在使用时会产生一个新线程,从而防止异常。

于 2013-04-08T07:11:25.517 回答
2

仅供参考android.os.NetworkOnMainThreadException,仅当您尝试在主 UI 线程上调用 Web 服务调用等长时间运行的进程时才会发生。它在 <2.3 的设备中效果最好,但如果您没有将它包含在 Threading 中并尝试在 >3.0 的设备上运行它,它会给您 NetworkOnMainThreadException。

要解决它,您可以实现 AsyncTask (标准方式)或包含以下代码(惰性方式):

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

在以下位置查看并阅读有关 NetworkOnMainThreadException 的更多信息:Android StrictMode – NetworkOnMainThreadException

于 2013-04-08T07:13:12.063 回答