2014-06-26 260 views
0

我在xmpp上使用asmack庫進行android聊天。以下是我用來連接到服務器的代碼。但該應用程序無法連接到服務器。 XMPPException服務器沒有響應。不過,我可以從桌面聊天登錄。我無法得到什麼問題。這是我用過的代碼。無法連接到服務器。 XMPPException服務器沒有響應

public class MainActivity extends Activity implements MessageListener{ 

    public static final String HOST = "mail.speedymx.com"; 
    public static final int PORT = 5222; 
    //public static final String SERVICE = "testService"; 
    public static final String USERNAME = "[email protected]"; 
    public static final String PASSWORD = "password"; 
     private Connection connection; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     System.setProperty("java.net.preferIPv6Addresses", "false"); 
     setContentView(R.layout.activity_main); 
       connect(); 
     connection.disconnect(); 
     Log.i("MyActivity", "disconnected from server"); 
    } 

    public void connect() { 

     final ProgressDialog dialog = ProgressDialog.show(this, 
       "Connecting...", "Please wait...",false, true); 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // Create a connection 
       System.setProperty("smack.debugEnabled", "true"); 
       ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT); 
       connConfig.setCompressionEnabled(true); 
       connConfig.setReconnectionAllowed(true); 
       connConfig.setDebuggerEnabled(true); 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
        connConfig.setTruststoreType("AndroidCAStore"); 
        connConfig.setTruststorePassword(null); 
        connConfig.setTruststorePath(null); 
       } else { 
        connConfig.setTruststoreType("BKS"); 
        String path = System.getProperty("javax.net.ssl.trustStore"); 
        if (path == null) 
         path = System.getProperty("java.home") + File.separator + "etc" 
          + File.separator + "security" + File.separator 
          + "cacerts.bks"; 
        connConfig.setTruststorePath(path); 
       } 

    connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
       SASLAuthentication.supportSASLMechanism("PLAIN"); 
       connConfig.setSASLAuthenticationEnabled(true); 
       connection = new XMPPConnection(connConfig); 
       try { 
        connection.connect(); 
        Log.i("MyActivity","Connected to "+ connection.getHost()+"connection is secured"+ connection.isSecureConnection()); 
       } 
    catch(XMPPException e) 
       { 
        Log.i("MyActivity", "connection error"+e.toString()); 
       } 
       try 
       { 
        connection.login(USERNAME, PASSWORD); 
        Log.i("MyActivity","Connected to "+ connection.getUser()); 

        runOnUiThread(new Runnable() { 
         public void run() { 
          dialog.dismiss(); 
         } 
        }); 
} 
       catch (XMPPException ex) { 
        Log.e("MyActivity", "Xmpp exception "+ USERNAME+ ex.toString()); 

        connection.disconnect(); 
        Log.i("MyActivity", "disconnected from server"); 
       } 
      `enter code here`catch (Exception ee) { 
        Log.e("MyActivity", ee.toString()); 
        connection.disconnect(); 
        Log.i("MyActivity", "disconnected from server"); 
       } 
      } 



     }).start(); 
     dialog.show(); 


     Presence presence = new Presence(Presence.Type.available); 
      presence.setStatus("available"); 
      presence.setPriority(24); 
     presence.setMode(Presence.Mode.available); 
     connection.sendPacket(presence); 

     sendmessgage(); 
private void sendmessgage() { 
    // TODO Auto-generated method stub 
    try { 
    Chat chat = connection.getChatManager().createChat("[email protected]", this); 
    chat.sendMessage("this is test message"); 
    } 
    catch (XMPPException e) { 
     Log.i("MyActivity","Error Delivering block"); 
     connection.disconnect(); 
    } 
} 

@Override 
public void processMessage(Chat arg0, Message arg1) { 
    // TODO Auto-generated method stub 
    Log.i("MyActivity", "Received message is "+ arg1); 

} 
} 

這裏是日誌中的其他參考

06-26 11:37:44.558: D/dalvikvm(421): GC_FOR_ALLOC freed 31K, 6% free 12337K/12999K, paused 88ms 
06-26 11:37:44.568: I/dalvikvm-heap(421): Grow heap (frag case) to 12.753MB for 358108-byte allocation 
06-26 11:37:44.650: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 6% free 12686K/13383K, paused 64ms 
06-26 11:37:44.837: D/dalvikvm(421): GC_FOR_ALLOC freed 215K, 6% free 12649K/13383K, paused 74ms 
06-26 11:37:44.847: I/dalvikvm-heap(421): Grow heap (frag case) to 12.936MB for 229392-byte allocation 
06-26 11:37:44.937: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 6% free 12872K/13639K, paused 80ms 
06-26 11:37:45.068: D/dalvikvm(421): GC_FOR_ALLOC freed 129K, 6% free 12858K/13639K, paused 64ms 
06-26 11:37:45.068: I/dalvikvm-heap(421): Grow heap (frag case) to 13.091MB for 178704-byte allocation 
06-26 11:37:45.168: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 6% free 13032K/13831K, paused 83ms 
06-26 11:37:45.318: D/dalvikvm(421): GC_FOR_ALLOC freed 98K, 7% free 12934K/13831K, paused 126ms 
06-26 11:37:45.338: I/dalvikvm-heap(421): Grow heap (frag case) to 14.098MB for 1155904-byte allocation 
06-26 11:37:45.448: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 7% free 14063K/14983K, paused 86ms 
06-26 11:37:45.618: D/dalvikvm(421): GC_CONCURRENT freed <1K, 7% free 14063K/14983K, paused 7ms+3ms 
06-26 11:37:45.899: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 7% free 14063K/14983K, paused 121ms 
06-26 11:37:45.918: I/dalvikvm-heap(421): Grow heap (frag case) to 16.058MB for 2054928-byte allocation 
06-26 11:37:46.052: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 6% free 16070K/17031K, paused 71ms 
06-26 11:37:46.238: D/dalvikvm(421): GC_CONCURRENT freed <1K, 6% free 16070K/17031K, paused 7ms+13ms 
06-26 11:37:46.477: D/AndroidRuntime(421): Shutting down VM 
06-26 11:37:46.477: W/dalvikvm(421): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
06-26 11:37:46.508: E/AndroidRuntime(421): FATAL EXCEPTION: main 
06-26 11:37:46.508: E/AndroidRuntime(421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.ChatBoss/com.org.ChatBoss.MainActivity}: java.lang.NullPointerException 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.os.Looper.loop(Looper.java:132) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread.main(ActivityThread.java:4025) 
06-26 11:37:46.508: E/AndroidRuntime(421): at java.lang.reflect.Method.invokeNative(Native Method) 
06-26 11:37:46.508: E/AndroidRuntime(421): at java.lang.reflect.Method.invoke(Method.java:491) 
06-26 11:37:46.508: E/AndroidRuntime(421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
06-26 11:37:46.508: E/AndroidRuntime(421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
06-26 11:37:46.508: E/AndroidRuntime(421): at dalvik.system.NativeStart.main(Native Method) 
06-26 11:37:46.508: E/AndroidRuntime(421): Caused by: java.lang.NullPointerException 
06-26 11:37:46.508: E/AndroidRuntime(421): at com.org.ChatBoss.MainActivity.connect(MainActivity.java:263) 
06-26 11:37:46.508: E/AndroidRuntime(421): at com.org.ChatBoss.MainActivity.onCreate(MainActivity.java:75) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
06-26 11:37:46.508: E/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 
06-26 11:37:46.508: E/AndroidRuntime(421): ... 11 more 
06-26 11:37:47.188: D/SMACK(421): 11:37:47 AM SENT (1087309296): <stream:stream to="mail.speedymx.com" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"> 
06-26 11:37:47.418: D/SMACK(421): 11:37:47 AM RCV (1087309296): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='2134678635' from='speedymx.com' xml:lang='en'><stream:error><host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream> 
06-26 11:37:47.488: D/SMACK(421): 11:37:47 AM SENT (1087309296): <presence id="NREU4-0" type="unavailable"></presence> 
06-26 11:37:47.508: I/MyActivity(421): Connected to mail.speedymx.comconnection is securedfalse 
06-26 11:37:47.518: D/SMACK(421): 11:37:47 AM SENT (1087309296): <iq id="NREU4-1" type="get"><query xmlns="jabber:iq:auth"><username>[email protected]</username></query></iq> 
06-26 11:37:47.528: D/SMACK(421): 11:37:47 AM SENT (1087309296): </stream:stream> 
06-26 11:37:47.678: W/System.err(421): stream:error (host-unknown) 
06-26 11:37:47.678: W/System.err(421): at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:255) 
06-26 11:37:47.678: W/System.err(421): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43) 
06-26 11:37:47.678: W/System.err(421): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69) 
06-26 11:37:52.574: E/MyActivity(421): Xmpp exception No response from the server.: 
06-26 11:37:52.787: I/MyActivity(421): disconnected from server 
06-26 11:37:53.878: I/Process(421): Sending signal. PID: 421 SIG: 9 
+0

嘗試使用斷點調試您的代碼並確保服務器正在運行 –

+1

實際上您已連接到服務器。看到這一行 - > 06-26 11:37:47.508:I/MyActivity(421):連接到mail.speedymx.comconnection是securedfalse –

+0

試圖..錯誤是在connection.login()。我只是想知道是否有東西丟失。是通過手動從桌面登錄來確保服務器正在工作 – user3643355

回答

0

日誌中的重要信息是:

<host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/> 

這是因爲你告訴你連接到mail.speedymx.com服務器,但服務器只知道speedymx.com。您應該將HOST設置爲正確的值。

如果speedymx.com的XMPP服務器實際上正在mail.speedymx.com上運行,那麼您應該添加指示該消息的SRV記錄。

+0

在相同的代碼我得到conncetion.sendpacket(存在)nullpointereception。你能建議如何刪除它 – user3643355

+0

正確拼寫連接嗎? – xnyhps

+0

我只是在寫評論時拼寫錯誤..請參考代碼存在狀態=新狀態(Presence.Type.available); presence.setStatus(「available」); presence.setPriority(24); presence.setMode(Presence.Mode.available); connection.sendPacket(presence); 我用正確的拼寫 – user3643355