尝试在Android上使用aSmack连接到XMPP服务器让我[否DNS解析活跃" [英] Trying to connect to XMPP server with aSmack on Android gets me "no dns resolver active"
问题描述
我做了这个简单,我可以在code。我使用asmack库的Android版本8-0.8.3。
I made this as simple as I can in code. I am using asmack library for android in version 8-0.8.3.
我的code:
package info.zajacmp3.servercommunication;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class XmppService extends Service{
public void xmppService() throws XMPPException {
Connection conn1 = new XMPPConnection("jabber.org");
conn1.connect();
}
@Override
public void onCreate(){
//TODO:actions to perform when service is created
try {
xmppService();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Replace with service binding
return null;
}
}
据冻结我的应用程序,使我和错误:没有DNS解析活跃。没有任何关于它在网络上。
It freeze my app and cause me and error: no dns resolver active. There is nothing on the web about it.
我也真的希望得到一些帮助或线索。
I do really hope to get some help or clues.
也试过这样的:
私人最终静态字符串SERVER_HOST =jabber.org;
私人最终静态INT SERVER_PORT = 5222;
private final static String server_host = "jabber.org"; private final static int SERVER_PORT = 5222;
公共无效xmppService()抛出XMPPException {
public void xmppService() throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration( server_host, SERVER_PORT);
XMPPConnection m_connection = new XMPPConnection(config);
try {
SASLAuthentication.supportSASLMechanism("PLAIN");
config.setSASLAuthenticationEnabled(true);
m_connection.connect();
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
} catch (XMPPException e) {
e.printStackTrace();
}
}
@UPDATE:
使用,而不是asmack嫌图书馆让我同样的问题。
我没有得到任何错误日志,但调试器断开后,我得到:
Using smack library instead of asmack gets me the same problem. I get no error log, but after disconnecting debugger I get:
推荐答案
我建议使用啪代替aSmack的。 Asmack是啪的修补和增强版本,aSmack是有点死了。在拍击code重构完成后,增加了一些新的方法和重构DNS类。
I recommend using Smack instead of aSmack. Asmack is a patched and enhanced version of Smack and aSmack is somewhat dead. In Smack code refactoring is done, added some new methods and refactored DNS classes.
请参阅此啪
更新结果
看到你的错误后登录这似乎是对主UI线程的网络呼叫
如何解决NetworkOnMainThread例外呢?的结果
使用AsyncTask的,这使得在不同的线程的网络呼叫(背景),而不是应用程序的主线程。
How to resolve NetworkOnMainThread exception?
Use AsyncTask which makes your network call on different thread(in background) rather than on app's main thread.
移动你的code到AsynTask: -
Move your code to AsynTask:-
private class ConnectToXmpp extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
ConnectionConfiguration config = new ConnectionConfiguration( server_host, SERVER_PORT);
XMPPConnection m_connection = new XMPPConnection(config);
try {
SASLAuthentication.supportSASLMechanism("PLAIN");
config.setSASLAuthenticationEnabled(true);
m_connection.connect();
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
} catch (XMPPException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
现在你可以执行你的AsyncTask: -
And now you can execute your AsyncTask:-
new ConnectToXmpp().execute();
通过这种网络电话将在不同的线程后台进行。
With this your network call will be made in background in different thread.
请参阅本 的AsyncTask
See this AsyncTask
这篇关于尝试在Android上使用aSmack连接到XMPP服务器让我[否DNS解析活跃&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!