简介使用smack实现xmpp通讯(一)
2014-05-13 00:16
183 查看
环境和工具:
1.openfire
2.spark
3.smack.jar
openfire和spark两个软件,都是直接安装就可以使用的,就没啥说的了。smack的jar包,可以去官网直接下载,一般导入smack和smackx的jar就可以了。
及时的文本消息通讯:
布局:
活动:
注意:
1.
远程服务器错误。针对使用模拟器的情况,如果服务端没问题的话,那就是你请求的ip地址跟服务器地址不匹配,把ip地址改为你的电脑ip试试。显然你的pc电脑作为服务器的话,localhost ip地址不是127的那个。
2. Could not read file
报只读的文件系统警告。看了下文件的权限,是075,所以就将文件创建的改为临时路径,就没问题了。
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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.acy_testsma); initView(); } public void initView() { myhandler = new Handler() { @Override public void handleMessage(android.os.Message msg) { // TODO Auto-generated method stub super.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 block e.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 block e.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() { @Override public void processMessage(Chat arg0, Message msg) { // TODO Auto-generated method stub Bundle 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 block e.printStackTrace(); } } } public void logOut() { if (connection != null) { connection.disconnect(); connection = null; } } class MyLogThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub if (conServer() && logIn("", "")) { // to do } else { } } } @Override public void onClick(View v) { // TODO Auto-generated method stub if (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,所以就将文件创建的改为临时路径,就没问题了。
相关文章推荐
- 使用smack基于xmpp服务实现即时通讯
- Android使用SharedPreferences实现登录帐号和密码的保存方法简介
- 学习笔记 --- LINNUX 使用异步通讯机制实现按键驱动代码分析
- Android四大组件应用系列——使用ContentProvider实现跨进程通讯
- 设计模式-观察者模式,以及如何使用观察者来为app实现即时通讯功能
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--类设计2[使用IE浏览本页]
- XMPP框架 微信项目开发之XMPP简介——XMPP的起源,工作原理,使用方法
- Android studio使用smack连接xmpp服务器收发消息
- [转载]在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分 .
- VMWare里Linux使用MINICOM实现与开发板的通讯
- 使用unity UGUI 利用Socket 实现 多客户端通讯
- 第二十六讲 使用ASP.NET实现网络通讯
- Java使用管道实现进程间通讯
- 基于binder的跨进程通讯之使用AIDL实现Demo
- 使用WebClient实现通讯(Silverlight学习笔记)
- 使用Java实现数据报通讯过程
- 使用SOCKET实现TCP/IP协议的通讯
- 人工神经网络简介和单层网络实现AND运算--AForge.NET框架的使用(五)
- 使用thrift实现java与node.js的通讯
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架