有时我们在做Flex展示的过程中,要测试网络是否可用,也即测试某个某个域名是否可用,这时我们可以通过JAVA服务器端后台程序进行测试。如果网络可用,则可进行相应的品作,如果不可用,则进行相关的处理。
package cn.iscas.ac.gz.struts.action;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import cn.iscas.ac.gz.common.file.WriteFile;
import cn.iscas.ac.gz.common.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
public class DetectNetAction extends ActionSupport {
private static final long serialVersionUID = 1L;
/**
* Logger for this class
*/
private static final Logger logger = Logger
.getLogger(FlexLogAction.class);
/**
* 日志
*/
private String log;
/**
* qone地址
*/
private String qoneUrl;
/**
* 社区地址
*/
private String bbsUrl;
/**
* 网络测试结果
*/
private String detectResult ;
/**
*
* @Title: saveLog
* @Description: 写日志
* @author huangpeng
* @date 2011-11-23 下午4:03:32
*/
public void saveLog() {
if (!StringUtil.isNullString(log)) {
WriteFile.write("D:\\flexLog.txt", log);
}
}
/**
*
* @Title: detectQoneNetwork
* @Description: 测试qone网络链接
* @author huangpeng
* @date 2011-11-23 下午4:09:58
*/
public String detectQoneNetwork() {
detectNetwork(qoneUrl);
return SUCCESS;
}
/**
*
* @Title: detectBbsNetwork
* @Description: 测试社区网络连接
* @author huangpeng
* @date 2011-11-23 下午4:09:37
*/
public String detectBbsNetwork() {
detectNetwork(bbsUrl);
return SUCCESS;
}
/**
*
* @Title: detectNetwork
* @Description: 测试网络地址
* @author huangpeng
* @date 2011-11-23 下午4:18:42
* @param urlAddress
*/
private void detectNetwork(String urlAddress) {
URL url = null;
try {
logger.info("检测网络地址:"+urlAddress);
url = new URL(urlAddress);
JSONObject jsonObject = new JSONObject();
try {
InputStream in = url.openStream();
in.close();
jsonObject.put("detectResult", 0);
} catch (IOException e) {
jsonObject.put("detectResult", -1);
}
detectResult = jsonObject.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}
logger.info("检测网络结束");
}
/**
* @return the log
*/
public String getLog() {
return log;
}
/**
* @param log
* the log to set
*/
public void setLog(String log) {
this.log = log;
}
/**
* @return the qoneUrl
*/
public String getQoneUrl() {
return qoneUrl;
}
/**
* @param qoneUrl
* the qoneUrl to set
*/
public void setQoneUrl(String qoneUrl) {
this.qoneUrl = qoneUrl;
}
/**
* @return the bbsUrl
*/
public String getBbsUrl() {
return bbsUrl;
}
/**
* @param bbsUrl
* the bbsUrl to set
*/
public void setBbsUrl(String bbsUrl) {
this.bbsUrl = bbsUrl;
}
/**
* @return the detectResult
*/
public String getDetectResult() {
return detectResult;
}
/**
* @param detectResult the detectResult to set
*/
public void setDetectResult(String detectResult) {
this.detectResult = detectResult;
}
}
某中返回: http://192.168.17.41:8080/Smarter/getQoneDetect给前台Flex使用。
在Flex部分:
qoneDetect = new HTTPService();
qoneDetect.addEventListener(ResultEvent.RESULT,qoneDetectHandler);
qoneDetect.url = Properties.IP_valuse+Properties.detectQone;
qoneDetect.send();
响应函数为:
public function qoneDetectHandler(event:ResultEvent):void{
var jsonData:String = new String(event.result);
//得到json数据
var arr:Object = (JSON.decode(jsonData) as Object);
var logDataFlag:String = arr.detectResult.toString();
trace("最开始检测:\t"+logDataFlag);
//0表示可以连接QONE,-1表示网络不可连
if(logDataFlag == "0"){
pageContentUrl = Properties.IFrameURL_value_Reflash_page;
}
//呈现第一个状态
toggleState("stateSeven");
Properties.pageTransferTimer=new Timer(Properties.ElapseTimer); //时间可定制
Properties.pageTransferTimer.addEventListener(TimerEvent.TIMER,PageTransferFunc);
//开启定时器
Properties.pageTransferTimer.start();
}
定时器中的函数:
protected function PageTransferFunc(event:TimerEvent):void{
//每隔一个时间去检测网络
qoneDetect1 =new HTTPService();
qoneDetect1.addEventListener(ResultEvent.RESULT,qoneDetectHandler1);
qoneDetect1.url = Properties.IP_valuse+Properties.detectQone;
qoneDetect1.send();
// qoneSocketConnect = SocketConnect.getInstence("58.249.121.93",8080);;
// communitySocketConnect = SocketConnect.getInstence("192.168.17.101",80);
// //每隔一个时间检测网络状态
// if(!qoneSocketConnect.isConnected || !communitySocketConnect.isConnected ){
// //Properties.pageTransferTimer.stop();
// //关闭定时器
//
// pageContentUrl= "welcome.html";
//
// //呈现第一个状态
// toggleState("stateSeven");
// //flag == false;
//
// // return ;
// }
//在这里做定时,一天或半天刷新一次界面
//Properties.initInterval();
// if (count == Properties.DayTimer) {
// count = 1;
// if (Properties.pageTransferTimer != null && Properties.pageTransferTimer.running) {
// Properties.pageTransferTimer.stop();
// }
//
// navigateToURL(new URLRequest("javascript:location.reload();"),"_self");
// } else {
// count ++;
// }
switch(mark){
case 1:
//displayAnimate(animate,"stateTwoPanel");
// anim.target="{[stateTwoPanel]}";
toggleState("stateSeven");
mark++;
break;
case 2:
// displayAnimate(animate,"stateThreePanel");
// anim.target="{[stateTwoPanel]}";
toggleState("stateOne");
mark++;
break;
case 3:
// displayAnimate(animate,"stateOnePanel");
// anim.target="{[stateOnePanel]}";
toggleState("stateThree");
mark++;
break;
case 4:
// displayAnimate(animate,"stateOnePanel");
// anim.target="{[stateOnePanel]}";
toggleState("stateFour");
mark++;
break;
case 5:
// displayAnimate(animate,"stateOnePanel");
// anim.target="{[stateOnePanel]}";
toggleState("stateFive");
// navigateToURL(new URLRequest("javascript:location.reload();"),"_self");
//navigateToURL(new URLRequest("javascript:location.close();"),"_self")
//关闭浏览器
//到了六的时候定时刷新页面一次
mark++;
break;
case 6:
toggleState("stateTwo");
mark+=1;
break;
case 7:
toggleState("stateEight");
mark+=1;
break;
case 8:
// displayAnimate(animate,"stateOnePanel");
// anim.target="{[stateOnePanel]}";
toggleState("stateSix");
// mark+=1;
mark=1;
break;
}
}
定时器中的响应函数:
public function qoneDetectHandler1(event:ResultEvent):void{
var jsonData:String = new String(event.result);
//得到json数据
var arr:Object = (JSON.decode(jsonData) as Object);
var logDataFlag:String = arr.detectResult.toString();
trace("定时器中检测:\t"+logDataFlag);
//0表示可以连接QONE,-1表示网络不可连
if(logDataFlag == "-1"){
Properties.pageTransferTimer.stop();
pageContentUrl = "Welcome.html";
//跳至第一张卡片
toggleState("stateSeven");
//开启定时器
}
// if(logDataFlag == "0"){
//
// pageContentUrl = Properties.IFrameURL_value_Reflash_page;
// //跳至第一张卡片
// toggleState("stateSeven");
//
// Properties.pageTransferTimer.start();
// }
}
(完...)