gpt4 book ai didi

java - 无法将简单的 Java Smack 连接到 Google Talk

转载 作者:行者123 更新时间:2023-11-30 03:08:19 25 4
gpt4 key购买 nike

我想在我的 android 应用程序中发送和接收 gmail 聊天。

我每次都在日志中得到 HostConnection::get() New Host Connection established 0x2a1bfc38, tid 16683。仅此而已。

下面是我的代码。

 XMPPConnection xmpp = new XMPPConnection("gmail.com");
try {
xmpp.connect();

// for other jabber accounts, truncate after the @
//xmpp.login("username", "password");

// for gtalk / gmail, include the @
xmpp.login("test04@gmail.com", "***");

Log.v("nirav", " connect to " + xmpp.getHost());

ChatManager chatmanager = xmpp.getChatManager();

Chat newChat = chatmanager.createChat("test04@gmail.com", new MessageListener() {
// THIS CODE NEVER GETS CALLED FOR SOME REASON
public void processMessage(Chat chat, Message message) {
try {
Log.v(TAG, "Got:" + message.getBody());
chat.sendMessage(message.getBody());
} catch (XMPPException e) {
Log.v(TAG, "Couldn't respond:" + e);
}
Log.v(TAG, message.toString());
}
});

try
{
newChat.sendMessage("adsd");
}
catch(Exception e)
{
Log.v(TAG, e.getMessage().toString());
}

// Accept only messages from friend@gmail.com
PacketFilter filter
= new AndFilter(new PacketTypeFilter(Message.class),
new FromContainsFilter("test@gmail.com"));

// Collect these messages
PacketCollector collector = xmpp.createPacketCollector(filter);

while(true) {
Packet packet = collector.nextResult();

if (packet instanceof Message) {
Message msg = (Message) packet;
// Process message
Log.v(TAG, "Got message:" + msg.getBody());
}
}

}
catch(Exception e)
{

}

jar文件列表:

jar file list for this project

最佳答案

这是用于在您的应用程序中发送和接收文本的 gmail 聊天应用程序的解决方案代码。

你应该试试这段代码。希望对您有所帮助。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

recipient = (EditText) this.findViewById(R.id.toET);
textMessage = (EditText) this.findViewById(R.id.chatET);
listview = (ListView) this.findViewById(R.id.listMessages);
setListAdapter();

// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.sendBtn);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String to = recipient.getText().toString();
String text = textMessage.getText().toString();

Log.i("XMPPChatDemoActivity", "Sending text " + text + " to " + to);
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
if (xmpp != null) {
xmpp.sendPacket(msg);
messages.add(xmpp.getUser() + ":");
messages.add(text);
setListAdapter();
}
}
});

connect();
}
public void setConnection(XMPPConnection connection) {
this.xmpp = connection;
if (xmpp != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
xmpp.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPChatDemoActivity", "Text Recieved " + message.getBody() + " from " + fromName );
messages.add(fromName + ":");
messages.add(message.getBody());
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
setListAdapter();
}
});
}
}
}, filter);
}
}
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, messages);
listview.setAdapter(adapter);
}
public void connect() {

final ProgressDialog dialog = ProgressDialog.show(this,
"Connecting...", "Please wait...", false);

Thread t = new Thread(new Runnable() {

@Override
public void run() {
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(
HOST, PORT, SERVICE);
XMPPConnection xmpp = new XMPPConnection(connConfig);

try {
xmpp.connect();
Log.i("XMPPChatDemoActivity",
"Connected to " + xmpp.getHost());
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to connect to "
+ xmpp.getHost());
Log.e("XMPPChatDemoActivity", ex.toString());
setConnection(null);
}
try {
// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
xmpp.login(USERNAME, PASSWORD);
Log.i("XMPPChatDemoActivity",
"Logged in as " + xmpp.getUser());

// Set the status to available
Presence presence = new Presence(Presence.Type.available);
xmpp.sendPacket(presence);
setConnection(xmpp);

Roster roster = xmpp.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.d("XMPPChatDemoActivity",
"--------------------------------------");
Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.d("XMPPChatDemoActivity",
"User: " + entry.getUser());
Log.d("XMPPChatDemoActivity",
"Name: " + entry.getName());
Log.d("XMPPChatDemoActivity",
"Status: " + entry.getStatus());
Log.d("XMPPChatDemoActivity",
"Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());

Log.d("XMPPChatDemoActivity", "Presence Status: "
+ entryPresence.getStatus());
Log.d("XMPPChatDemoActivity", "Presence Type: "
+ entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.d("XMPPChatDemoActivity", "Presence : "
+ entryPresence);

}
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "Failed to log in as "
+ USERNAME);
Log.e("XMPPChatDemoActivity", ex.toString());
setConnection(null);
}

dialog.dismiss();
}
});
t.start();
dialog.show();
}

关于java - 无法将简单的 Java Smack 连接到 Google Talk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404150/

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