消息接收线程

来源:互联网 发布:淘宝旺旺客服链接生成 编辑:程序博客网 时间:2024/06/05 06:34

/**
 * [项目名]: 教育局桌面驻留程序
 * [模块名]: 消息接收线程
 * [文件名]: MessageThread.java
 * [文件实现功能]: 定时接收后台的消息
 * [作者]: ztc * [版本]: v1.0
 * [版权所有]:
 * -------
 * [备注]:
 *
 * -------
 * [修改记录]:
 * [ 日    期 ]     [版本]      [修 改 人]       [修 改 内 容]
 *  2013/09/06       1.0         ztc         创建
 * [遗留问题]:
 */
package com.education.view;
import com.education.bean.res;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

public class MessageThread extends Thread {

    @Override
    public void run() {
        while (true) {
            /**
             * 接收消息提醒
             */
            System.out.println(res.name);
            //向服务器请求消息数据
            byte[] returnByte = CustomerHttpClient.post(res.http + "/OA/oatool.do?method=getInformation&p_userId=" + res.name + "", null);
            if (null == returnByte || 0 == returnByte.length) {
                continue;
            }
            String ServerResponse = null;
            try {
                ServerResponse = new String(returnByte, "utf-8");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(MessageThread.class.getName()).log(Level.SEVERE, null, ex);
                continue;
            }
            try {//解析JSONA数据
                JSONArray jsonArray = new JSONArray(ServerResponse);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = (JSONObject) jsonArray.get(i);
                    String msg = (String) jsonObj.get("msg");//消息内容
                    String url = (String) jsonObj.get("url");//消息url
                    //弹出消息提示框
                    new MessageBox(msg, url);
                }
            } catch (Exception ex) {
            }
            try {
                Thread.sleep(res.messageThreadTime * 1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(MessageThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

原创粉丝点击