尝试使用Android上的Smack连接到XMPP服务器可以让我“没有dns解析器活动” [英] Trying to connect to XMPP server with aSmack on Android gets me "no dns resolver active"

查看:319
本文介绍了尝试使用Android上的Smack连接到XMPP服务器可以让我“没有dns解析器活动”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中尽可能简单。我在版本8-0.8.3中使用Android的asmack库。



我的代码:

  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:创建服务时执行的操作
try {
xmppService();
} catch(XMPPException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

@Override
public IBinder onBind(Intent intent){
// TODO替换为服务绑定
return null;
}
}

它冻结我的应用程序,导致我错误:否dns解析器激活。网络上没有任何内容。



我真的希望得到一些帮助或线索。



也尝试这样:



private final static String server_host =jabber.org;
private final static int SERVER_PORT = 5222;



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:



使用smack库而不是asmack让我遇到同样的问题。
我没有收到错误日志,但在断开调试器之后,我得到:



解决方案

我建议使用Smack而不是aSmack。 Asmack是一个补丁和增强版本的Smack和aSmack有点死了。在Smack代码重构中,添加了一些新的方法和重构的DNS类。



看到这个 Smack



更新

看到您的错误日志后似乎是主UI线程上的网络调用的问题



如何解决NetworkOnMainThread异常?

使用AsyncTask,使您的网络在不同的线程上(在后台)而不是应用程序的主线程。



移动代码到AsynTask: -

 私有类ConnectToXmpp扩展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();
}

返回null;
}

@Override
protected void onPostExecute(Void result){

}

}

现在,您可以执行AsyncTask: -

  new ConnectToXmpp()。execute(); 

这样您的网络呼叫将以不同线程的背景进行。



请参阅此 AsyncTask


I made this as simple as I can in code. I am using asmack library for android in version 8-0.8.3.

My 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;
    }
}

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.

Also tried like this:

private final static String server_host = "jabber.org"; private final static int SERVER_PORT = 5222;

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:

Using smack library instead of asmack gets me the same problem. I get no error log, but after disconnecting debugger I get:

解决方案

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.

See this Smack

UPDATE
After seeing your error log it seems to be the problem of network call on main UI thread

How to resolve NetworkOnMainThread exception?
Use AsyncTask which makes your network call on different thread(in background) rather than on app's main thread.

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) {

    }

}

And now you can execute your AsyncTask:-

new ConnectToXmpp().execute();

With this your network call will be made in background in different thread.

See this AsyncTask

这篇关于尝试使用Android上的Smack连接到XMPP服务器可以让我“没有dns解析器活动”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆