gpt4 book ai didi

java - 连接到 xmpp 出现套接字异常(权限被拒绝)

转载 作者:行者123 更新时间:2023-11-29 19:59:57 26 4
gpt4 key购买 nike

我正在尝试使用 smack 库通过我的 android 应用程序连接到 openfire 服务器。当我连接到服务器时,它给了我错误

以下地址失败:'192.168.0.31:5222' 失败,因为 java.net.SocketException: socket failed: EACCES (Permission denied)

但是我已经在 list 中授予了 Internet 权限。

我使用的代码是..

public void init(String mUsername, String mPassword) {


Log.i(TAG, "connect()");
config = XMPPTCPConnectionConfiguration.builder();
config.setServiceName(mServiceName);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setHost(mServiceName);
config.setPort(5222);
config.setDebuggerEnabled(true);
config.setResource("sender");
// config.setCompressionEnabled(true);
config.setUsernameAndPassword(mUsername, mPassword);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);


mConnection = new XMPPTCPConnection(config.build());
mConnection.addConnectionListener(this);
ChatManager.getInstanceFor(mConnection).addChatListener(this);


gson = new Gson();
connectAndLoginAnonymously();
}

public void connectAndLoginAnonymously() {
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
mConnection.connect();
DeliveryReceiptManager dm = DeliveryReceiptManager
.getInstanceFor(mConnection);
dm.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
dm.addReceiptReceivedListener(new ReceiptReceivedListener() {

@Override
public void onReceiptReceived(final String fromid,
final String toid, final String msgid,
final Stanza packet) {

}
});
mConnection.login();
} catch (SmackException | XMPPException | IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void res) {
}
};

// execute AsyncTask
mRegisterTask.execute();
}




public void disconnect() {
Log.i(TAG, "disconnect()");
if (mConnection != null) {
mConnection.disconnect();
}
}

//For Creating new User.
public boolean createNewAccount(String username, String newpassword) {
boolean status = false;
if (mConnection == null) {
try {
mConnection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}

try {
// String newusername = username + mConnection.getServiceName();
Log.i("service", mConnection.getServiceName());
AccountManager accountManager = AccountManager.getInstance(mConnection);
accountManager.createAccount(username, newpassword);
status = true;
} catch (SmackException.NoResponseException e) {
status = false;
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
status = false;
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
status = false;
}
mConnection.disconnect();
return status;

}

当我调用 smackConnection.init(user,password); 时从我的登录 Activity 它给我 java.net.SocketException permission denied 错误。

错误日志如下。

04-05 18:28:18.142 9575-9575/dhaval.com.chatdemo I/SMACK: connect()
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err:

org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '192.168.0.31:5222' failed because java.net.SocketException: socket failed: EACCES (Permission denied)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:605)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:839)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:365)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at dhaval.com.chatdemo.SmackConnection$1.doInBackground(SmackConnection.java:104)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at dhaval.com.chatdemo.SmackConnection$1.doInBackground(SmackConnection.java:100)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-05 18:28:18.152 9575-9604/dhaval.com.chatdemo W/System.err: at java.lang.Thread.run(Thread.java:841)

list 代码:

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

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

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

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

我是不是做错了什么?任何帮助将不胜感激。

最佳答案

希望您的 list 中缺少 uses-permission INTERNET。

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

关于java - 连接到 xmpp 出现套接字异常(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36427412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com