gpt4 book ai didi

java - XMPP 和 Android 第 2 部分

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:09 27 4
gpt4 key购买 nike

这个问题是我上一个问题的延续,尽管我已经设法删除了 NetworkOnMainThreadError,但现在我有新的错误需要解决,

上一个问题可以在这里找到:XMPP and ANDROID

这次我猜问题是我的主机名,或者可能是服务名,我正在尝试连接到 openFire 服务器。

顺便说一句,我需要将 public class SettingsDialog extends Dialog 添加到 Menifest 文件

我的连接代码:

/**
* Gather the xmpp settings and create an XMPPConnection
*/
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
private XMPPClient xmppClient;

public SettingsDialog(XMPPClient xmppClient) {
super(xmppClient);
this.xmppClient = xmppClient;
}

protected void onStart() {
super.onStart();
setContentView(R.layout.settings);
getWindow().setFlags(4, 4);
setTitle("XMPP Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}


public void onClick(View v) {

new NetworkOperation().execute();
dismiss();
}

异步任务

public class NetworkOperation extends AsyncTask<String, Void, String> {

private XMPPClient xmppClient;
@Override
protected String doInBackground(String... urls) {

String host = "web.vlivetech.com"; //getText(R.id.host);
String port = "5222"; //getText(R.id.port);
String service = "web.vlivetech.com"; //getText(R.id.service);
String username = "khubaib@web.vlivetech.com"; //getText(R.id.userid);
String password = "khubaib"; //getText(R.id.password);

// Create a connection
ConnectionConfiguration connConfig =
new ConnectionConfiguration(host, Integer.parseInt(port),service);
XMPPConnection connection = new XMPPConnection(connConfig);

try {
connection.connect();
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
xmppClient.setConnection(null);
}
try {
connection.login(username, password);
Log.i("XMPPClient", "Logged in as " + connection.getUser());

// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
xmppClient.setConnection(connection);
} catch (XMPPException ex) {
Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
xmppClient.setConnection(null);
}

return null;
}

@Override
protected void onPostExecute(String result) {

}
}

我在异步任务中遇到错误的行:

try {
connection.connect();
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
xmppClient.setConnection(null);
}

日志文件

03-06 17:50:39.754: I/SmackConfiguration(16410): No configuration file found
03-06 17:50:39.764: E/XMPPClient(16410): [SettingsDialog] Failed to connect to null
03-06 17:50:39.764: W/dalvikvm(16410): threadid=11: thread exiting with uncaught exception (group=0x40aaa210)
03-06 17:50:39.774: E/AndroidRuntime(16410): FATAL EXCEPTION: AsyncTask #1
03-06 17:50:39.774: E/AndroidRuntime(16410): java.lang.RuntimeException: An error occured while executing doInBackground()
03-06 17:50:39.774: E/AndroidRuntime(16410): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-06 17:50:39.774: E/AndroidRuntime(16410): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.lang.Thread.run(Thread.java:856)
03-06 17:50:39.774: E/AndroidRuntime(16410): Caused by: java.lang.NullPointerException
03-06 17:50:39.774: E/AndroidRuntime(16410): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:33)
03-06 17:50:39.774: E/AndroidRuntime(16410): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:1)
03-06 17:50:39.774: E/AndroidRuntime(16410): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-06 17:50:39.774: E/AndroidRuntime(16410): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

最佳答案

如果您的 NetworkOperation 位于您的 Activity 内,则从 NetworkOperation 中删除此行,或者您可能想要添加一个构造函数(?),通过xmppClient.execute()

private XMPPClient xmppClient;

关于java - XMPP 和 Android 第 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225167/

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