欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

即时通讯开发中报的异常:-- caused by: java.net.ConnectException: failed to connect to /127.0

程序员文章站 2022-06-26 18:13:05
...

在使用Openfire作为服务器进行即时通讯(IM)开发的时候报出了一下异常:

 

XMPPError connecting to 127.0.0.1:5222.: remote-server-error(502) XMPPError connecting to 127.0.0.1:5222.
   -- caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 5222): connect failed: ECONNREFUSED (Connection refused)

 

 

异常代码:

 

08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err: XMPPError connecting to 127.0.0.1:5222.: remote-server-error(502) XMPPError connecting to 127.0.0.1:5222.
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:   -- caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 5222): connect failed: ECONNREFUSED (Connection refused)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:566)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:998)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at com.jxust.asus.xmpp.activity.LoginActivity$1$1.run(LoginActivity.java:78)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.lang.Thread.run(Thread.java:818)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err: Nested Exception: 
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err: java.net.ConnectException: failed to connect to /127.0.0.1 (port 5222): connect failed: ECONNREFUSED (Connection refused)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.net.Socket.connect(Socket.java:882)
08-04 11:55:50.786 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.net.Socket.connect(Socket.java:825)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:50)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:554)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:998)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at com.jxust.asus.xmpp.activity.LoginActivity$1$1.run(LoginActivity.java:78)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at java.lang.Thread.run(Thread.java:818)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err: Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at libcore.io.Posix.connect(Native Method)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
08-04 11:55:50.787 15149-15709/com.jxust.asus.xmpp W/System.err: 	... 9 more

 

此时,你的第一反应可能是某出代码有问题或者是清单文件中某个权限忘加了,其实不是这样的。原来默认把127.0.0.1localhost当做本身了,在模拟器上可以用10.0.2.2代替127.0.0.1localhost,另外如果是在局域网环境可以用 192.168.0.x或者192.168.1.x(根据具体配置)连接本机,这样应该就不会报错了。

 

 

具体实例代码:

 

package com.jxust.asus.xmpp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jxust.asus.xmpp.R;
import com.jxust.asus.xmpp.utils.ThreadUtils;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class LoginActivity extends Activity {

    private TextView mEtUsername;
    private TextView mEtPassword;
    private Button mBtnLogin;

    public static final String HOST = "10.0.2.2";      // 主机IP
    public static final int PORT = 5222;           // 对应的端口号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.i("main", "onCreate");
        initView();
        initListener();
    }

    private void initView() {
        mEtUsername = (TextView) findViewById(R.id.et_username);
        mEtPassword = (TextView) findViewById(R.id.et_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        Log.i("main", "InitView");
    }

    private void initListener() {
        Log.i("main", "initListener");
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = mEtUsername.getText().toString();
                final String password = mEtPassword.getText().toString();
                // 判断用户名是否为空
                if (TextUtils.isEmpty(userName)) {    // 说明用户名为空
                    mEtUsername.setError("用户名不能为空");
                    return;
                }
                // 判断密码是否为空
                if (TextUtils.isEmpty(password)) {    // 说明密码为空
                    mEtPassword.setError("密码不能为空");
                    return;
                }

                // 在子线程里面进行连接操作
                ThreadUtils.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // 创建XMPP连接配置对象
                            ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);

                            // 额外的配置,方便我们开发,上线的时候再改回来
                            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);  // 明文传输数据,方便开发调试
                            config.setDebuggerEnabled(true);        // 开启调试模式,方便我们查看具体发送的内容

                            // 开始创建连接
                            XMPPConnection conn = new XMPPConnection(config);

                            // 开始连接
                            conn.connect();

                            // 连接成功了
                            // 开始登录
                            conn.login(password, userName);

                            // 已经登录成功
                            ThreadUtils.runInUIThread(new Runnable() {  // 回到主线程
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (XMPPException e) {
                            e.printStackTrace();
                            // 已经登录成功
                            ThreadUtils.runInUIThread(new Runnable() {  // 回到主线程
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                });
            }
        });
    }
}

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jxust.asus.xmpp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.LoginActivity"></activity>
    </application>

</manifest>

 

 

相关标签: XMPP Android