android - XMPP客户端无法连接到XMPP localhost服务器

时间:2011-12-20 05:46:24

标签: android debugging xmpp

我在android中有以下代码 - 我正在使用asmack API。

 public XMPPManager(String server, int port) {
    this.server = server;
    this.port = port;

    SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
    config = new ConnectionConfiguration(server, port);
    config.setSASLAuthenticationEnabled(false);
    config.setSecurityMode(SecurityMode.disabled);
    connection = new XMPPConnection(config);
    try {
      connection.connect();
      connection.login(user, pass);
    } catch (XMPPException e) {
      Log.w("Cannot connect to server.", "0");
      e.printStackTrace();
    }
    if (connection.isAuthenticated()) {
      AccountManager manager = connection.getAccountManager();
      try {
        manager.createAccount("test", "test");
      } catch (XMPPException e) {
        Log.w("Cannot create new user -----" + " on the XMPP server.", "0");
      }
    }

  }

我正在XMPPClient活动中创建一个XMPPManager类的实例,如下所示:

public class XMPPClient extends Activity {
  private String username = "user";
  private String password = "pass";
  private String host     = "127.0.0.1";
  private int    port     = 5222;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    try {
      super.onCreate(savedInstanceState);

      XMPPManager xmppManager = new XMPPManager(host, port);
    } catch (Exception e) {

    }
  }

}

我遇到的问题如下:

W/Cannot connect to server.( 1052): 0
W/System.err( 1052): XMPPError connecting to 127.0.0.1:5222.: remote-server-error(502) XMPPError connecting to 127.0.0.1:5222.
W/System.err( 1052):   -- caused by: java.net.ConnectException: /127.0.0.1:5222 - Connection refused
W/System.err( 1052):    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:562)
W/System.err( 1052):    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:991)
W/System.err( 1052):    at company.games.boxer.XMPPManager.<init>(XMPPManager.java:45)
W/System.err( 1052):    at company.games.boxer.XMPPClient.onCreate(XMPPClient.java:17)
W/System.err( 1052):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 1052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
W/System.err( 1052):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W/System.err( 1052):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
W/System.err( 1052):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
W/System.err( 1052):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 1052):    at android.os.Looper.loop(Looper.java:123)
W/System.err( 1052):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err( 1052):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 1052):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 1052):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err( 1052):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err( 1052):    at dalvik.system.NativeStart.main(Native Method)
W/System.err( 1052): Nested Exception:
W/System.err( 1052): java.net.ConnectException: /127.0.0.1:5222 - Connection refused
W/System.err( 1052):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254)
W/System.err( 1052):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
W/System.err( 1052):    at java.net.Socket.connect(Socket.java:1055)
W/System.err( 1052):    at java.net.Socket.connect(Socket.java:997)
W/System.err( 1052):    at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
W/System.err( 1052):    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:550)
W/System.err( 1052):    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:991)
W/System.err( 1052):    at company.games.boxer.XMPPManager.<init>(XMPPManager.java:45)
W/System.err( 1052):    at company.games.boxer.XMPPClient.onCreate(XMPPClient.java:17)
W/System.err( 1052):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 1052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
W/System.err( 1052):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W/System.err( 1052):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
W/System.err( 1052):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
W/System.err( 1052):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 1052):    at android.os.Looper.loop(Looper.java:123)
W/System.err( 1052):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err( 1052):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 1052):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 1052):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err( 1052):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err( 1052):    at dalvik.system.NativeStart.main(Native Method)

为什么我收到“无法连接到服务器”的任何想法。错误?提前谢谢。

2 个答案:

答案 0 :(得分:6)

我想这一行,

private String host = "127.0.0.1";

导致异常..所以试试..

private String host = "10.0.2.2";系统的公共IP

Bco'z Android不理解你的系统的127.0.0.1 ip作为本地主机,这个ip用于模拟器本身..(循环地址)

答案 1 :(得分:0)

检查您的xmpp服务器是否配置正确,并且您可以连接到它。您可以使用任何其他第三方xmpp客户端连接&amp;测试你的服务器。