【发布时间】:2016-08-03 08:22:15
【问题描述】:
我在使用 Smack 和 Openfire 发送/接收消息时遇到了问题。目前我的设置涉及两台计算机。两台计算机都运行一个启动程序的安卓模拟器。该程序首先启动登录活动,然后启动聊天界面活动。聊天界面目前不做任何事情;一旦 ChatInterface 活动启动,就会自动发送一条消息。
一台计算机(笔记本电脑)充当服务器,台式计算机连接到笔记本电脑服务器(Openfire)。查看笔记本电脑上的 Openfire 用户摘要,两个用户似乎都可用并准备好聊天。但是,当它接收消息时,什么也没有出现。消息不会打印在 ListView 等视图对象上,而是打印在日志上。
以下是笔记本电脑代码:
private static final String TAG = "MSG";
private ListView screen;
private EditText textEditor;
private Button sendButton;
private final String Host = "10.0.2.2";
private final int port = 5222;
private AbstractXMPPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_interface);
screen = (ListView) findViewById(R.id.ScreenView);
textEditor = (EditText) findViewById(R.id.TextEditBox);
sendButton = (Button) findViewById(R.id.SendButton);
Log.d(TAG, "onCreate Chat Interface");
final String username = getIntent().getExtras().getString("Username");
final String password = getIntent().getExtras().getString("Password");
new AsyncConnect().execute(username, password);
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
Log.d(TAG, "Success");
}
public class AsyncConnect extends AsyncTask<String, Void, Void>
{
ProgressDialog pdLoading = new ProgressDialog(ChatInterface.this);
protected void onPreExecute()
{
pdLoading.setMessage("Loading...");
pdLoading.show();
}
protected Void doInBackground(String...params)
{
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(params[0], params[1]);
configBuilder.setServiceName("Openfire");
configBuilder.setHost(Host);
configBuilder.setPort(port);
configBuilder.build();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // Remove this later could be security threat
connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
connection.login();
Log.d(TAG, "Logged on");
Log.d(TAG, "Connection: " + connection.getUser());
if(params[0].equals("test2") && params[1].equals("test2!")) {
Log.d(TAG, "Entered test2");
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Log.d(TAG, "ChatManager");
Chat newChat = chatManager.createChat("LoveJack@yahoo.com", new ChatMessageListener(){
public void processMessage(Chat chat, Message message)
{
Log.d(TAG, "Received Message From Desktop: " + message.getBody());
}
});
newChat.sendMessage("This is Jane Doe");
chatManager.addChatListener(
new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
Log.d(TAG, "Entered chatCreated");
if (!createdLocally) {
Log.d(TAG, "Entered Locally");
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
try {
Log.d(TAG, "Incoming message...");
Log.d(TAG, "Received message from Desktop: " + message.getBody());
Log.d(TAG, "Message Received");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
});
Log.d(TAG, "Listened");
}
}
catch(SmackException.ConnectionException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException.ConnectionException");
}
catch(XMPPException e)
{
e.printStackTrace();
Log.d(TAG, "XMPPException");
}
catch(IOException e)
{
e.printStackTrace();
Log.d(TAG, "IOException");
}
catch(SmackException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException");
}
return null;
}
protected void onPostExecute(Void params)
{
pdLoading.dismiss();
}
}
以下是桌面代码:
private static final String TAG = "MSG";
private ListView screen;
private EditText textEditor;
private Button sendButton;
private final String Host = "192.168.1.152";
private final int port = 5222;
private AbstractXMPPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_interface);
screen = (ListView) findViewById(R.id.ScreenView);
textEditor = (EditText) findViewById(R.id.TextEditBox);
sendButton = (Button) findViewById(R.id.SendButton);
Log.d(TAG, "onCreate Chat Interface");
final String username = getIntent().getExtras().getString("Username");
final String password = getIntent().getExtras().getString("Password");
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
}
});
new AsyncConnect().execute(username, password);
Log.d(TAG, "Success");
}
public class AsyncConnect extends AsyncTask<String, Void, Void> {
ProgressDialog pdLoading = new ProgressDialog(ChatInterface.this);
protected void onPreExecute() {
pdLoading.setMessage("Loading...");
pdLoading.show();
}
protected Void doInBackground(String...params) {
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(params[0], params[1]);
configBuilder.setServiceName("Openfire");
configBuilder.setHost(Host);
configBuilder.setPort(port);
configBuilder.build();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); // Remove this later could be security threat
connection = new XMPPTCPConnection(configBuilder.build());
try {
connection.connect();
connection.login();
Log.d(TAG, "Logged on");
Log.d(TAG, "Connection: " + connection.getUser());
if(params[0].equals("test1") && params[1].equals("test1!"))
{
Log.d(TAG, "Entered test1");
ChatManager chatmanager = ChatManager.getInstanceFor(connection);
Log.d(TAG, "ChatManager");
Chat newChat = chatmanager.createChat("HumbleBee001@yahoo.com", new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.d(TAG, message.getBody());
}
});
newChat.sendMessage("This is John Doe");
Log.d(TAG, newChat.getParticipant());
Log.d(TAG, "Message sent");
}
}
catch(SmackException.ConnectionException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException.ConnectionException");
}
catch(XMPPException e)
{
e.printStackTrace();
Log.d(TAG, "XMPPException");
}
catch(IOException e)
{
e.printStackTrace();
Log.d(TAG, "IOException");
}
catch(SmackException e)
{
e.printStackTrace();
Log.d(TAG, "SmackException");
}
return null;
}
protected void onPostExecute(Void params)
{
pdLoading.dismiss();
}
}
没有调用 processMessages 方法,因为 logcat 中没有打印任何内容。
我不太确定问题出在哪里。一些 Smack 文档似乎已经过时了,并且没有很多很好的 API 资源。有人知道可能是什么问题吗?
【问题讨论】:
标签: java android openfire smack