我正在学习Android编程,而且我一直在努力解决这个问题。我正在编写和应用程序连接到XMPP服务器的Android应用程序。我总是得到同样的错误而且不知道我做错了什么。我尝试过使用谷歌找到的示例代码,但无法与他们建立连接。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
SmackAndroid.init(context);
ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration("192.168.0.101", 5222);
ConnectionConfiguration.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPTCPConnection(ConnectionConfiguration);
try {
connection.connect();
} catch (ConnectionException e) {
Log.e("ERROR", "ConnectionException", e);
for (int i = 0; i < e.getFailedAddresses().size(); i++) {
HostAddress element = e.getFailedAddresses().get(i);
Log.e("ERROR", element.getErrorMessage().toString());
}
} catch (Exception e) {
Log.e("ERROR", "Exception", e);
}
// Log into the server
try {
connection.login("user", "password", "SomeResource");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我得到的错误:
08-11 15:48:27.476: E/ERROR(11117): ConnectionException
08-11 15:48:27.476: E/ERROR(11117): org.jivesoftware.smack.SmackException$ConnectionException
08-11 15:48:27.476: E/ERROR(11117): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:433)
08-11 15:48:27.476: E/ERROR(11117): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:808)
08-11 15:48:27.476: E/ERROR(11117): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
08-11 15:48:27.476: E/ERROR(11117): at com.example.gps_pubsub.MainActivity.onCreate(MainActivity.java:34)
08-11 15:48:27.476: E/ERROR(11117): at android.app.Activity.performCreate(Activity.java:5047)
08-11 15:48:27.476: E/ERROR(11117): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-11 15:48:27.476: E/ERROR(11117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
08-11 15:48:27.476: E/ERROR(11117): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
08-11 15:48:27.476: E/ERROR(11117): at android.app.ActivityThread.access$700(ActivityThread.java:134)
08-11 15:48:27.476: E/ERROR(11117): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
08-11 15:48:27.476: E/ERROR(11117): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 15:48:27.476: E/ERROR(11117): at android.os.Looper.loop(Looper.java:137)
08-11 15:48:27.476: E/ERROR(11117): at android.app.ActivityThread.main(ActivityThread.java:4867)
08-11 15:48:27.476: E/ERROR(11117): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 15:48:27.476: E/ERROR(11117): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 15:48:27.476: E/ERROR(11117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-11 15:48:27.476: E/ERROR(11117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-11 15:48:27.476: E/ERROR(11117): at dalvik.system.NativeStart.main(Native Method)
08-11 15:48:27.476: E/ERROR(11117): 192.168.0.101:5222 Exception: null
我还检查过防火墙是否允许连接并尝试连接到hosted.im服务而不是家庭服务器,结果相同。
I have this line added to MANIFEST:
<uses-permission android:name="android.permission.INTERNET"/>
答案 0 :(得分:10)
我遇到了同样的问题,但在这里找到了解决方案: SImple Asmack program not working
解决方案是将连接代码放入单独的线程中。
public static final String HOST = "208.68.163.218"; //write your host name
public static final int PORT = 5222;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect();
}
public void connect() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Context context = getApplicationContext();
SmackAndroid.init(context);
ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration(HOST, PORT);
ConnectionConfiguration.setDebuggerEnabled(true);
XMPPConnection connection = new XMPPTCPConnection(ConnectionConfiguration);
try {
connection.connect();
} catch (ConnectionException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
t.start();
}
答案 1 :(得分:1)
即使代码的连接部分在新线程中,我也得到了这个异常。 这是因为我的物理安卓设备上没有打开我的互联网连接(通过wifi或移动数据网络)(我使用它通过USB线连接到我的笔记本电脑)(不知道是否可以在模拟器上运行连接,没有测试)。我在api结构深处发现了这个异常抛出的异常:
connect failed: ENETUNREACH (Network is unreachable)
此异常未在LogCat中显示,但最后在阅读Flow对此问题的评论后,我在HostAddress列表中找到了它。
也许这可以帮助别人拼命寻找像我这样的代码中的错误。