模擬器運行正常,真機 (N6600, Symbian OS) 執行 HttpConnection POST 請求時拋出 Symbian OS error = -5120。
public static String getHTTPInfo() {
HttpConnection hc = null;
DataInputStream dis = null;
StringBuffer sb = new StringBuffer();
try {
hc = (HttpConnection) Connector.open("https://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");
int rc = hc.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
return "網絡故障,錯誤碼:" + rc;
dis = new DataInputStream(hc.openDataInputStream());
int ch, count = 0;
while ((ch = dis.read()) != -1 && count++ < 50)
sb.append((char)ch);
return toGb(sb.toString()); // 假設轉 GB 碼
} catch (Exception e) {
return "錯誤:" + e.getMessage();
} finally {
closeQuietly(dis); closeQuietly(hc);
}
}
| 錯誤碼 | 含義 |
|---|---|
| -5120 | KErrInUse / 端口衝突、協議棧忙碌 |
| -44 | KErrNotSupported / 不支援的協議或參數 |
| -18 | KErrNoMemory / 記憶體不足 |
APN,否則預設使用 WAP 閘道導致協議不匹配。MIDlet-Connection-UID 或讓用戶手動選擇接入點。Content-Length。hc.setRequestProperty("Content-Length", String.valueOf(body.length))。close() 後再發起新請求。HttpConnection hc = null;
try {
hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
hc.setRequestMethod(HttpConnection.GET); // 簡單查詢優先 GET
hc.setRequestProperty("Connection", "close"); // 避免 Keep-Alive 導致閘道超時
int rc = hc.getResponseCode();
if (rc == HttpConnection.HTTP_OK) {
try (InputStream is = hc.openInputStream()) {
// 讀取邏輯...
}
}
} finally {
if (hc != null) try { hc.close(); } catch (IOException ignored) {}
}
-5120 多為「協議棧資源佔用」或「WAP 閘道協商失敗」。優先檢查 APN、簡化請求頭、改用 GET、確保串行化連接管理,多數情況可解決。