简介使用smack实现xmpp通讯(一)
来源:互联网 发布:mac虚拟机 360优化 编辑:程序博客网 时间:2024/05/22 05:14
环境和工具:
1.openfire
2.spark
3.smack.jar
openfire和spark两个软件,都是直接安装就可以使用的,就没啥说的了。smack的jar包,可以去官网直接下载,一般导入smack和smackx的jar就可以了。
及时的文本消息通讯:
布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="log In" /> <Button android:id="@+id/btn_sendmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send Msg" /> <Button android:id="@+id/btn_logout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="log Out" /></LinearLayout>
活动:
public class TestSmack extends Activity implements OnClickListener {private Button userLogin;private Button userSendmsg;private Button userLogout;private Connection connection;private Handler myhandler;File file = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.acy_testsma);initView();}public void initView() {myhandler = new Handler() {@Overridepublic void handleMessage(android.os.Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);String txt = null;switch (msg.what) {case 0:txt = msg.getData().getString("msg");Toast.makeText(TestSmack.this, txt, 1000).show();break;default:break;}}};userLogin = (Button) findViewById(R.id.btn_login);userLogin.setOnClickListener(this);userSendmsg = (Button) findViewById(R.id.btn_sendmsg);userSendmsg.setOnClickListener(this);userLogout = (Button) findViewById(R.id.btn_logout);userLogout.setOnClickListener(this);}/** * 链接服务器 * * @return */public boolean conServer() {try {// 这里的服务器地址 为pc的ip地址ConnectionConfiguration config = new ConnectionConfiguration("192.168.1.103", 5222);connection = new XMPPConnection(config);connection.connect();return true;} catch (XMPPException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}/** * 登录 * * @param username * @param userpwd * @return */public boolean logIn(String username, String userpwd) {if (connection != null) {try {connection.login("admin", "admin");return true;} catch (XMPPException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return false;}/** * 发送消息 * * @param username * @param txt */public void sendMsg(String username, String txt) {if (connection != null) {try {Chat chat = connection.getChatManager().createChat("user@jin/Smack", new MessageListener() {@Overridepublic void processMessage(Chat arg0, Message msg) {// TODO Auto-generated method stubBundle bundle = new Bundle();bundle.putString("msg", msg.getBody());android.os.Message message = new android.os.Message();message.what = 0;message.setData(bundle);myhandler.sendMessage(message);}});chat.sendMessage("hello world");} catch (XMPPException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public void logOut() {if (connection != null) {connection.disconnect();connection = null;}}class MyLogThread implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubif (conServer() && logIn("", "")) {// to do} else {}}}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == userLogin) {MyLogThread myLog = new MyLogThread();Thread thread = new Thread(myLog);thread.start();} else if (v == userSendmsg) {sendMsg("", "");} else if (v == userLogout) {if (null != file && file.exists()) {file.delete();file = null;}logOut();}}}
注意:
1.
127.0.0.1:5222 Exception: XMPPError connecting to 127.0.0.1:5222.; : remote-server-error(502)
远程服务器错误。针对使用模拟器的情况,如果服务端没问题的话,那就是你请求的ip地址跟服务器地址不匹配,把ip地址改为你的电脑ip试试。显然你的pc电脑作为服务器的话,localhost ip地址不是127的那个。
2. Could not read file
报只读的文件系统警告。看了下文件的权限,是075,所以就将文件创建的改为临时路径,就没问题了。
1 0
- 简介使用smack实现xmpp通讯(一)
- 使用smack基于xmpp服务实现即时通讯
- XMPP学习(一)smack类库的使用及XMPP的了解
- XMPP简介(一)
- XMPP通讯过程简介
- Android基于XMPP Smack openfire 开发的聊天室(一)
- 基于xmpp openfire smack 开发(一)群聊
- Android基于XMPP Smack openfire 开发的聊天室(一)
- 一.xmpp openfire smack spark介绍
- Android基于XMPP协议,Smack类库的IM通讯
- Smack实现XMPP即时通信--接收消息
- Android使用XMPP协议、Openfire服务器和Smack类库实现即时通信
- XMPPFrameWork IOS 开发(一)xmpp简介
- XMPPFrameWork IOS 开发(一)xmpp简介
- XMPPFrameWork IOS 开发(一)xmpp简介
- XMPPFrameWork IOS 开发(一)xmpp简介
- Android基于XMPP Smack openfire 开发的聊天室(一)【会议服务、聊天室列表、加入】
- Android基于XMPP Smack openfire 开发的聊天室(一)【会议服务、聊天室列表、加入】
- 通过微信谈产品
- 关于uboot的makefile 和 make zImage
- Tree Summing - UVa 112 简单二叉树(丧病的数据输入)
- Java Getter方法返回类对象
- 数字图像处理的就业前景与学习资源
- 简介使用smack实现xmpp通讯(一)
- UVA之1398 - Meteor
- java回调函数
- 为Eclipse定制你自己的注释模板变量
- jquerymobile 学习笔记——新手常见问题
- Log的使用
- 填一个坑。关于把ruby编译到android上的一个遗留问题。
- jdbc连接数据库
- 《计算机程序的构造与解释》(一)