才接觸J2ME,寫一個訪問網頁的小程序,
查找了多方的資料,我下面使用的連接都是正確的,在模擬器上運行也正常,為什麼到了真機上就返回
SymBianos error = - 5120錯誤,
public static String getHTTPInfo() {
String re = "";
HttpConnection hc = null;
DataInputStream dis = null;
int rc; // as httpconnection error type
StringBuffer messagebuffer = new StringBuffer();
try {
hc = (HttpConnection) Connector.open("http://www.google.cn/");
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("If-ModifIEd-Since","29 Oct 1999 19:43:31 GMT");
hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
rc = hc.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
return "網絡故障,錯誤碼:" + rc;
}
dis = new DataInputStream(hc.openDataInputStream());
long len; // 返回內容長度
int ch; //
len = hc.getLength();
if (len != -1) {
// 內容太長,會引起內存溢出,所以最多只取前50個字符
if (len <= 50) {
for (int i = 0; i < len; i++) {
if ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
} else {
for (int i = 0; i < 50; i++) {
if ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
}
re = togb(messagebuffer.toString());
} else {
int j = 0;
while ((ch = dis.read()) != -1) {
// 內容太長,會引起內存溢出,所以最多只取前50個字符
if (j >= 50) {
break;
}
messagebuffer.append((char) ch);
j++;
}
re = togb(messagebuffer.toString());
}
dis.close();
return re;
} catch (Exception e) {
return "錯誤:" + e.getMessage();
} finally {
if (hc != null)
try {
hc.close();
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
if (dis != null)
try {
dis.close();
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}