繁体   English   中英

Asmack Android与服务器Xmpp的连接错误

[英]Error in Asmack Android Connection to Server Xmpp

我无法连接到服务器,我不知道为什么请帮助我。 这是我的代码:

public class Sample extends Activity{

    /** Called when the activity is first created. */
    TextView tvHello;
    XMPPTCPConnection connection;
    ConnectionConfiguration config;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvHello = (TextView) findViewById(R.id.tvHello);
        Log.i("ohyeah", "I'm here");
        config = new ConnectionConfiguration("host", 5222, "servername");
        connection = new XMPPTCPConnection(config);
        try {

           connection.connect();
            // tvHello.setText("Connected to XMPP server");
            Log.i("ohyeah", "Successfully Connected");
        } catch (XMPPException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("ohyeah", "Not Connected");
        } catch (SmackException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("ohyeah", "Something Fishy");
        } catch (IOException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("ohyeah", "yes");
        }
    }
}

这是我的错误: http : //i.stack.imgur.com/iaRdO.png

您不能在ui线程中执行长时间运行或后台运行的进程,因此请尝试使用AsyncTask连接xmpp服务器:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvHello = (TextView) findViewById(R.id.tvHello);
    Log.i("ohyeah", "I'm here");
    connectToXmppServer();
 }

public void connectToXmppServer(){
    new AsyncTask<Void,Void,String>(){
       @Override
       protected String doInBackground(Void... params) {
           config = new ConnectionConfiguration("host", 5222, "servername");
           connection = new XMPPTCPConnection(config);
           try {
               connection.connect();
               // tvHello.setText("Connected to XMPP server");
               Log.i("ohyeah", "Successfully Connected");
           } catch (XMPPException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               Log.e("ohyeah", "Not Connected");
           } catch (SmackException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               Log.i("ohyeah", "Something Fishy");
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               Log.i("ohyeah", "yes");
           }
           return null;
        }
   }.execute();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM