无法连接到服务器。 XMPPException服务器没有响应

时间:2014-06-26 07:39:42

标签: android xmpp chat asmack

我在xmpp上使用asmack库进行android聊天。下面是我用来连接服务器的代码。但该应用程序无法连接到服务器。 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 = "username@speedymx.com";
    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("samreshs@speedymx.com", 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>username@speedymx.com</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

1 个答案:

答案 0 :(得分:0)

日志中的重要信息是:

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

这是因为您告诉服务器您正在连接mail.speedymx.com,但服务器只知道speedymx.com。您应该将HOST设置为正确的值。

如果speedymx.com的XMPP服务器实际上正在mail.speedymx.com上运行,那么您应该添加指示该值的SRV记录。