- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 smack api 和 talk.google.com 服务器创建一个 android 聊天应用程序,但问题是当我使用 gmail id 和密码创建到 Smack API 的连接并检查我的用户时使用“connection.getuser()”的名称我的 ID 以以下格式显示“anything@gmail.com/Smack_(一些 smack id)”。现在如果我使用另一部手机从我的应用程序发送消息到“anything@gmail.com”,但没有收到,但如果我将它发送到“anything@gmail.com/Smack_(相同smack id)”它被正确接收。
附加的smack id是随机的,每次连接都会改变,所以我无法通过任何方式获得它,因此无法发送消息,请问如何解决这个问题。
我的 ChatActivity 的代码是:
package com.prince.chat;
import java.util.ArrayList;
import java.util.Collection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ChatActivity extends Activity {
public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
public static String USERNAME ;
public static String PASSWORD ;
private XMPPConnection connection;
private ArrayList<String> messages = new ArrayList<String>();
private Handler mHandler = new Handler();
private EditText recipient;
private EditText textMessage;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
recipient = (EditText) this.findViewById(R.id.toET);
textMessage = (EditText) this.findViewById(R.id.chatET);
listview = (ListView) this.findViewById(R.id.listMessages);
setListAdapter();
Intent i = getIntent();
USERNAME= i.getStringExtra("email");
PASSWORD =i.getStringExtra("password");
// 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 (connection != null) {
connection.sendPacket(msg);
messages.add(connection.getUser() + ":");
messages.add(text);
setListAdapter();
}
}
});
connect();
}
/**
* Called by Settings dialog when a connection is establised with
* the XMPP server
*/
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.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);
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
connection.disconnect();
} catch (Exception e) {
}
}
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 connection = new XMPPConnection(connConfig);
try {
connection.connect();
Log.i("XMPPChatDemoActivity", "[SettingsDialog] Connected to "+connection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPChatDemoActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.e("XMPPChatDemoActivity", ex.toString());
setConnection(null);
}
try {
connection.login(USERNAME, PASSWORD);
Log.i("XMPPChatDemoActivity", "Logged in as" + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);
Roster roster = connection.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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chat, menu);
return true;
}
}
最佳答案
您可以在连接时将资源 ID 设置为某个已知值,因此不会使用生成的 ID(除非服务器更改它,我相信如果它愿意,它可以这样做)。
关于java - 使用 Smack API 和 talk.goolgle.com 服务器进行 Android 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18984588/
Google Talk如何工作?当我连接到我的 friend 进行谈话时,什么充当 中级 一个转移我的声音?那是它背后的谷歌服务器(充当中间人?还是提供点对点协议(protocol)? 最佳答案 他们
我有两个(Java)进程在不同的 JVM 上重复运行。第一个经常发现一些“信息”并需要将其存储在某个地方。第二个进程定期读取此信息来处理它。这些间隔或多或少是随机的,因此进程 1 可能会找到三条信息,
我想创建一些服务,该服务将作为简单进程运行,并为其他应用程序提供向他发送 xml 流的可能性。 我的意思是创建具有无限循环的简单进程 (exe) - 任何应用程序都能够将 XML(文件/流)发送到该进
我想知道(官方)GTalk 客户端如何设法显示接收到的所有 消息——即使它最初是由另一个客户端使用的。例如:我在笔记本电脑上登录 gmail.com 上的 GTalk,同时在我的 Android 设备
Google GTalk 客户端(web、android、windows)具有从服务器获取对话历史的强大能力,即使对话是在另一个 jabber 客户端完成的。 什么 jabber (XMPP) 功能
我知道一旦调用函数,this 的上下文就会更改为对象,但如何在无需创建方法的情况下模拟该上下文? var object = { x: 1, y: object.x, //
Google Talk 如何播放当前歌曲?我可以使用 .net 执行此操作吗? 编辑:看起来每个玩家都有自己的 SDK 供使用 温安普 :这是一个关于要使用的类的好帖子:http://forums.w
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
当我们为android或ios应用程序安装代理时,我们通常会添加一些库并更改一些配置设置。仅添加一个库如何使该应用程序记录事件并将其推送到 monkey talk IDE。 通常我们在项目中添加库的时
我是一名大学生。我正在上我的第二个 comp sci 类(class),我们还没有太多简单地在其中制作类和函数,所以我还没有真正能够充分利用我在互联网上找到的复杂行话。 我正在制作蟑螂出没模拟游戏,我
我正在开发一个 Android 应用程序,其中显示了 Google Talk 联系人及其状态 - 这工作正常。 我不确定应该调用哪个 Intent 来使用默认的 Talk 应用打开聊天窗口。 我如何知
我一直在努力弄清楚 TED Talk homepage 到底是怎么回事作品。撇开所有的动画垃圾不谈,我发现盒子的组织方式真的很迷人。 乍一看,它看起来像 jQuery masonry plugin ,
我想知道如何通过特殊客户端为 Google Talk 创建聊天机器人。 我知道它使用 XMPP 发送消息,但我完全不知道如何使用它。据我了解,如果我要创建自己的客户端页面,我应该能够制作一个机器人,当
几天来,我一直在尝试通过 SmartBand Talk (SWR30) 发送 Google 文字转语音音频。我对 AndroidManifest.xml 拥有正确的权限 PERMISSION_CONT
我设置了一个在预览控制台中运行的 GoogleHome 操作 我会写“用行动说话” 我得到我的答案 如何用法语测试 GoogleHome 上的该操作?任何解决方法? 我尝试在移动应用程序中使用别名来触
我正在尝试使用PHP连接到Google Talk XMPP服务器。我成功建立了连接并使用X-OAUTH2登录。然后Google要求我建立TLS连接。但是,当尝试将连接升级到TLS时,出现异常:stre
我是扩展程序编程的新手,但发现在 Chrome 中制作扩展要容易得多,但现在我正在尝试将我的测试扩展移植到 FF,我有几个问题如何做与我在 Chrome 中所做的相同的事情。 .现在在FF。 例如:在
FB 公开了此 EdgeRank 指标(显示在每个页面上) - 现在如何访问它来收集统计数据? 最佳答案 几周前我向 Facebook 团队询问了此事;看来当时它被忽视了,他们会调查它。我今天刚刚检查
我正在尝试使用 Monkey Talk 进行自动化测试。我安装成功了。我在 Eclipse Juno 上运行示例应用程序,然后创建一个新的 Monkey 脚本并记录操作。 问题是 Monkey IDE
有谁知道 Google Talk 浏览器插件是否可供网络应用程序开发人员使用?我说的是用 HTML/JavaScript 来操作它。 Google Talk 开发网站似乎只为想要开发独立客户端/服务器
我是一名优秀的程序员,十分优秀!