我正在尝试从我的Android设备通过XMPPClient
连接到开放式服务器。我在Network thread
上单独运行Async-Task
。
问题是
1 - 我的代码不允许我连接到openfire服务器,但它很容易与gmail服务器连接。我的代码中缺少一些东西吗?
2 - 我的系统与任何服务器连接的时刻,例如gmail,崩溃,我在LogCat
错误
03-07 12:29:57.085: W/dalvikvm(26662): threadid=11: thread exiting with uncaught exception (group=0x40a95210)
03-07 12:29:57.115: E/AndroidRuntime(26662): FATAL EXCEPTION: AsyncTask #1
03-07 12:29:57.115: E/AndroidRuntime(26662): java.lang.RuntimeException: An error occured while executing doInBackground()
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.lang.Thread.run(Thread.java:856)
03-07 12:29:57.115: E/AndroidRuntime(26662): Caused by: java.lang.NullPointerException
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:42)
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:1)
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
以下是在异步任务
中运行的代码import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.os.AsyncTask;
import android.util.Log;
public class NetworkOperation extends AsyncTask<String, Void, String> {
private XMPPClient xmppClient;
@Override
protected String doInBackground(String... urls) {
String host = "talk.google.com"; //getText(R.id.host);
String port = "5222"; //getText(R.id.port);
String service = "gmail.com"; //getText(R.id.service);
String username = "hassaan.rabbani@gmail.com"; //getText(R.id.userid);
String password = "**********"; //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) {
}
}
我错过了什么吗? N'
答案 0 :(得分:0)
看起来你永远不会为你的xmppClient变量赋值,因此它是null。
你没有在第一次尝试中击中它,因为那是成功的。
答案 1 :(得分:0)
尝试使用以下代码。
@Override
protected String doInBackground(String... urls) {
String host = "talk.google.com"; //getText(R.id.host);
String port = "5222"; //getText(R.id.port);
String service = "gmail.com"; //getText(R.id.service);
String username = "hassaan.rabbani@gmail.com"; //getText(R.id.userid);
String password = "**********"; //getText(R.id.password);
// Create a connection
ConnectionConfiguration connConfig =
new ConnectionConfiguration(host, Integer.parseInt(port),service);
XMPPConnection connection = new XMPPConnection(connConfig);
if(xmppClient != null){
try {
connection.connect();
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
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 connect to " + connection.getHost());
xmppClient.setConnection(null);
}
}else{
Log.d("Connection", "check your xmppClient");
}
return null;
}