环境和工具:
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,所以就将文件创建的改为临时路径,就没问题了。