无法连接到服务器。 XMPPException 服务器无响应
Posted
技术标签:
【中文标题】无法连接到服务器。 XMPPException 服务器无响应【英文标题】:Unable to connect to server. XMPPException No response from server 【发布时间】:2014-06-26 07:39:42 【问题描述】:我在 xmpp 上使用 asmack 库进行 android 聊天。下面是我用来连接服务器的代码。但是该应用程序。但是我可以从桌面聊天登录。我无法理解问题所在。这是我使用的代码。
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 ComponentInfocom.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
【问题讨论】:
尝试使用断点调试代码并确保服务器正在运行 实际上您已连接到您的服务器。看到这一行 -> 06-26 11:37:47.508: I/MyActivity(421): Connected to mail.speedymx.comconnection issecuredfalse 试过了.. connection.login() 出现错误。我只是想知道是否缺少某些东西。是的,通过从桌面手动登录来确保服务器正常工作 @WaqarAhmed 我使用过 connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);仍然当我打印 connection.isSecured 它给出错误。我不知道为什么 您使用的是哪个 aSmack 版本? 【参考方案1】:日志中的重要信息是:
<host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
这是因为你告诉服务器你正在连接到mail.speedymx.com
,但服务器只知道speedymx.com
。您应该将HOST
设置为正确的值。
如果 speedymx.com
的 XMPP 服务器实际上在 mail.speedymx.com
上运行,那么您应该添加 SRV 记录来表明这一点。
【讨论】:
在相同的代码中,我在 conncetion.sendpacket(presence) 上获得了 nullpointereception。你能建议如何删除它 尝试正确的拼写连接? 我只是在写评论时拼错了.. 请参考代码 Presence presence = new Presence(Presence.Type.available);存在.setStatus(“可用”);存在.setPriority(24); Presence.setMode(Presence.Mode.available); connection.sendPacket(存在);我使用了正确的拼写 我找到了答案.. 在主类上实现 messgae 监听器产生了问题以上是关于无法连接到服务器。 XMPPException 服务器无响应的主要内容,如果未能解决你的问题,请参考以下文章
Redis 客户端未通过 HAProxy 连接到 Redis 服务器