?? networkdetector.java
字號:
package net.sourceforge.j2meautonetwork.operation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.sourceforge.j2meautonetwork.util.CodeDefine;
import net.sourceforge.j2meautonetwork.util.Utilities;
/**
* 網(wǎng)絡(luò)探測器.
* @author Steven King(woyaoying@hotmail.com)
* @version 0.3
*/
public final class NetworkDetector implements Runnable {
private String proxy;
private boolean connected;
private DataInputStream httpdis = null;
private DataOutputStream httpdos = null;
private HttpConnection httpconn = null;
private String errorMsg;
private boolean runningOver = false;
/**
* 嘗試連接網(wǎng)絡(luò).
* @param originalUrl
* @throws Throwable
*/
private void tryConnect(String originalUrl) throws Throwable {
String hostName, servlet, url;
if (originalUrl.toLowerCase().indexOf("http://") < 0) {
originalUrl = "http://" + originalUrl;
}
int pos1 = "http://".length();
int pos2 = originalUrl.indexOf('/', pos1);
if (pos2 < 0) {
hostName = originalUrl.substring(pos1);
servlet = "";
} else {
hostName = originalUrl.substring(pos1, pos2);
servlet = originalUrl.substring(pos2);
}
int pos3 = hostName.indexOf(":");
if (pos3 < 0) {
hostName += ":80";
}
if (proxy != null) {
url = proxy + servlet;
} else {
url = "http://" + hostName + servlet;
}
httpconn = (HttpConnection) Connector.open(url);
if (proxy != null) {
httpconn.setRequestProperty(CodeDefine.HTTP_PROXY_HOST_HEADER,
hostName);
httpconn.setRequestProperty(CodeDefine.HTTP_ACCEPT[0],
CodeDefine.HTTP_ACCEPT[1]);
httpconn.setRequestProperty(CodeDefine.HTTP_USER_AGENT[0],
CodeDefine.HTTP_USER_AGENT[1]);
}
debug("URL: " + url);
try {
if (httpconn != null) {
httpdis = httpconn.openDataInputStream();
httpdos = httpconn.openDataOutputStream();
} else {
throw new Exception("HttpConnection 不能為空");
}
} finally {
closeConnection();
}
}
private void closeConnection(){
long time = System.currentTimeMillis();
if (httpdis != null) {
try {
httpdis.close();
httpdis = null;
} catch (Throwable e) {
e.printStackTrace();
}
}
debug("close httpdis end " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
if (httpdos != null) {
try {
httpdos.close();
httpdos = null;
} catch (Throwable e) {
e.printStackTrace();
}
}
debug("close httpdos end " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
if (httpconn != null) {
try {
// httpconn.close(); Dont user close, it will be block so long time.
httpconn = null;
} catch (Throwable e) {
e.printStackTrace();
}
}
debug("close httpconn end " + (System.currentTimeMillis() - time));
}
private synchronized void setConnected() {
runningOver = true;
connected = true;
debug("Connected true");
}
public synchronized void notifyStop() {
connected = false;
runningOver = true;
debug("超時!!!!");
long time = System.currentTimeMillis();
closeConnection();
debug("Close connection " + (System.currentTimeMillis() - time));
//setOver(CodeDefine.NETWORK_ERRORS[1]);
}
private void debug(String log) {
Utilities.debug("Detector "+ proxy + " " + log);
}
public void run() {
//timer.schedule(new TimeOutMonitor(this), CodeDefine.HTTP_CONNECT_TIME_OUT);
try {
tryConnect("http://wap.baidu.com:80");
//timer.cancel();
setConnected();
} catch (Throwable e) {
if (e instanceof SecurityException) {
debug("請同意連接網(wǎng)絡(luò),重新啟動應(yīng)用.");
}
Utilities.debug("catch try except " + e.getMessage());
//timer.cancel();
} finally{
closeConnection();
}
}
public String getProxy() {
return proxy;
}
public synchronized void setOver() {
runningOver = true;
}
public NetworkDetector(String proxy) {
this.proxy = proxy;
}
public String getErrorMsg() {
return errorMsg;
}
public synchronized boolean getConnected() {
return connected;
}
public boolean getOver() {
return runningOver;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -