/**
* Created by huang on 2016/12/3.
*/
public class ProtacolObjc implements Serializable {
public String toXml() {
XStream stream = new XStream();
//将根节点转换为类名
stream.alias(this.getClass().getSimpleName(), this.getClass());
return stream.toXML(this);
}
public Object fromXml(String xml) {
XStream x = new XStream();
x.alias(this.getClass().getSimpleName(), this.getClass());
return x.fromXML(xml);
}
//创建Gson数据和字符串之间转换的方法,适应多种数据
public String toGson() {
Gson gson = new Gson();
return toGson();
}
public Object fromGson(String result) {
Gson gson = new Gson();
return gson.fromJson(result, this.getClass());
}
}
创建线程工具,指定方法运行在子线程和主线程中。由于网络操作需要在子线程中,界面更新需要在主线程中,创建线
程工具可以方便选择线程。
import android.os.Handler;
/**
* Created by huang on 2016/12/5.
*/
public class ThreadUtils {
private static Handler handler = new Handler();
public static void runUIThread(Runnable r){
handler.post(r);
}
public static void runINThread(Runnable r){
new Thread(r).start();
}
}
创建消息的工具类,包括消息内容、消息类型、消息本省等。由于服务器返回的内容中包含消息的包名信息所以消息本
身的包名应该于服务其保持一直。
/**
* Created by huang on 2016/12/3.
* 消息内容
*/
public class QQMessage extends ProtacolObjc {
public String type = QQmessageType.MSG_TYPE_CHAT_P2P;// 类型的数据 chat login
public long from = 0;// 发送者 account
public String fromNick = "";// 昵称
public int fromAvatar = 1;// 头像
public long to = 0; // 接收者 account
public String content = ""; // 消息的内容 约不?
public String sendTime = getTime(); // 发送时间
public String getTime() {
Date date = new Date(System.currentTimeMillis());
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("mm-DD HH:mm:ss");
return format.format(date);
}
public String getTime(Long time) {
Date date = new Date(time);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("mm-DD HH:mm:ss");
return format.format(date);
}
}
/**
* Created by huang on 2016/12/3.
* 消息类型
*/
public class QQmessageType {
public static final String MSG_TYPE_REGISTER = "register";// 注册
public static final String MSG_TYPE_LOGIN = "login";// 登录
public static final String MSG_TYPE_LOGIN_OUT = "loginout";// 登出
public static final String MSG_TYPE_CHAT_P2P = "chatp2p";// 聊天
public static final String MSG_TYPE_CHAT_ROOM = "chatroom";// 群聊
public static final String MSG_TYPE_OFFLINE = "offline";// 下线
public static final String MSG_TYPE_SUCCESS = "success";//成功
public static final String MSG_TYPE_BUDDY_LIST = "buddylist";// 好友
public static final String MSG_TYPE_FAILURE = "failure";// 失败
}
import com.example.huang.imsocket.bean.ProtacolObjc;
/*
*消息本身 包括 账号、头像和昵称
*
*/
public class QQBuddy extends ProtacolObjc {
public long account;
public String nick;
public int avatar;
}
/**
* Created by huang on 2016/12/3.
*/
public class QQBuddyList extends ProtacolObjc {
public ArrayList buddyList = new ArrayList<>();
}
关于socket的创建连接和发送消息、接受消息。
import android.util.Log;
import com.example.huang.imsocket.bean.QQMessage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by huang on 2016/12/3.
* 连接 服务器
*/
public class QQConnection extends Thread {
private static final String TAG = "QQConnection";
private Socket client;
private DataOutputStream write;
private DataInputStream read;
public static final String HOST = "192.168.23.48";
public static final int POST = 5225;
private boolean flag = true;
private List mOnQQmwssagereceiveLisener = new ArrayList<>();
public void addOnQQmwssagereceiveLisener(OnQQmwssagereceiveLisener lisener) {
mOnQQmwssagereceiveLisener.add(lisener);
}
public void removeOnQQmwssagereceiveLisener(OnQQmwssagereceiveLisener lisener) {
mOnQQmwssagereceiveLisener.remove(lisener);
}
public interface OnQQmwssagereceiveLisener {
public void onReiceive(QQMessage qq);
}
@Override
public void run() {
super.run();
while (flag) {
try {
String utf = read.readUTF();
QQMessage message = new QQMessage();
QQMessage msg = (QQMessage) message.fromXml(utf);
if (msg != null) {
for (OnQQmwssagereceiveLisener lisner : mOnQQmwssagereceiveLisener)
lisner.onReiceive(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void connect() {
try {
if (client == null) {
client = new Socket(HOST, POST);
write = new DataOutputStream(client.getOutputStream());
read = new DataInputStream(client.getInputStream());
flag = true;
this.start();
Log.e(TAG, "connect: "+(write==null)+"---"+ (read == null));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnect() {
if (client != null) {
flag = false;
this.stop();
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
write.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void send(String xml) throws IOException {
write.writeUTF(xml);
write.flush();
}
public void send(QQMessage qq) throws IOException {
write.writeUTF(qq.toXml());
write.flush();
}