J2me联网线程
J2me联网线程
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**
* Runnable类必须实现的方法
*/
public void run() {
String connStr = preURLStr + urlArgs;
if (wapConnFlag) {
boolean getDataFlag = false;
HttpConnection hcon = null;
DataInputStream dis = null;
try {
hcon = (HttpConnection) Connector.open(connStr, Connector.READ_WRITE);
Tools.logMsg("请求的URL为: " + connStr);
if (!InputForm.NotQuickChangeSubInquireStateFlag) {
mainCanvas.doWithGetString("遇到网络错误", false);
return;
}
hcon.setRequestProperty("X-Online-Host", IPADDR + ":" + PORT);
hcon.setRequestMethod(HttpConnection.GET);
hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
StringBuffer getBuffer = new StringBuffer();
int ch;
dis = hcon.openDataInputStream();
while ((ch = dis.read()) != -1) {
getBuffer.append((char) ch);
}
String getStr = new String(getBuffer.toString().getBytes("ISO8859-1"), "UTF-8");
getBuffer = null;
Tools.logMsg("接收的数据为: " + getStr);
if (hcon.getResponseCode() == HttpConnection.HTTP_OK) {
if (!getStr.startsWith("<?xml")) {
mainCanvas.doWithGetString(getStr, true);
getStr = null;
getDataFlag = true;
}
}
} catch (Exception e) {
} finally {
try {
if (hcon != null) {
hcon.close();
hcon = null;
}
if (dis != null) {
dis.close();
dis = null;
}
} catch (Exception ioe) {
}
}
if (getDataFlag) {
connStr = null;
return;
}
}
boolean aliveFlag = true;
int connectTime = 0;
while (aliveFlag) {
HttpConnection hcon = null;
DataInputStream dis = null;
try {
hcon = (HttpConnection) Connector.open(connStr, Connector.READ_WRITE);
Tools.logMsg("请求的URL为: " + connStr);
if (wapConnFlag) {
hcon.setRequestProperty("X-Online-Host", IPADDR + ":" + PORT);
}
hcon.setRequestMethod(HttpConnection.GET);
hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
StringBuffer getBuffer = new StringBuffer();
int ch;
dis = hcon.openDataInputStream();
while ((ch = dis.read()) != -1) {
getBuffer.append((char) ch);
}
String getStr = new String(getBuffer.toString().getBytes("ISO8859-1"), "UTF-8");
getBuffer = null;
Tools.logMsg("接收的数据为: " + getStr);
int responseCode = hcon.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
mainCanvas.doWithGetString("遇到网络错误,响应码为" + responseCode, false);
} else {
if (!getStr.startsWith("<?xml")) {
aliveFlag = false;
mainCanvas.doWithGetString(getStr, true);
getStr = null;
}
}
} catch (IllegalArgumentException iae) {
// mainCanvas.doWithGetString("网络连接失败", false);
// aliveFlag = false;
} catch (ConnectionNotFoundException cnfe) {
mainCanvas.doWithGetString("请确保服务器正常运行", false);
aliveFlag = false;
} catch (IOException ioe) {
// mainCanvas.doWithGetString("遇到网络问题", false);
// aliveFlag = false;
} catch (SecurityException se) {
mainCanvas.doWithGetString("请赋予程序联网权限", false);
aliveFlag = false;
} catch (Exception e) {
mainCanvas.doWithGetString(e.toString(), false);
} finally {
try {
if (hcon != null) {
hcon.close();
hcon = null;
}
if (dis != null) {
dis.close();
dis = null;
}
} catch (Exception ioe) {
}
}
if (aliveFlag) {
connectTime++;
if (connectTime >= 4) {
mainCanvas.doWithGetString("遇到网络错误", false);
aliveFlag = false;
}
}
}
connStr = null;
}