2013-04-07 69 views
2

我很抱歉格式提前。 我是新十岁上下的Android开发,我上的XMPP客户合作,同服务器在家里办通过谷歌的命令谈论机器人....aSmack崩溃与NetworkOnMainThreadException

  1. 我没看计算器的一些答案并通过github上
  2. 一派一些答案
  3. 我更新到最新ASMACK LIB
  4. 我没有添加使用许可
  5. 我ASMACK就在我的Android测试应用程序的工作。

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

我有一个名为'Setting Dialog'的设置对话框。当我按一下按钮我运行此:

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 = "[email protected]";//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(); 
      } 

    } 
    ); 
} 

,当我按下按钮调用的主要活动

public void setConnection (XMPPConnection XMPPCon) {

this.XMPPCon = XMPPCon; 
    if (XMPPCon != null) 
    { 
     ChatManager chatmanager = XMPPCon.getChatManager(); 
     Chat newChat = chatmanager.createChat("[email protected]",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 [email protected] 
      PacketFilter filter 
       = new AndFilter(new PacketTypeFilter(Message.class), 
           new FromContainsFilter("[email protected]")); 

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

我得到这个错误此一功能“setConnection”

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)

+0

...有它的工作 – 2013-04-08 01:45:46

回答

2

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

要解决它,你可以实现的AsyncTask(一种标准方式)或包括下面的代码(偷懒的方法):

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

检查和了解更多关于NetworkOnMainThreadException在:启动一个新的线程需要Android StrictMode – NetworkOnMainThreadException

2

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

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

其中,当使用时,将产生一个新的线程,并且因此防止了异常。