在
诺基亚s40 Asha 305设备中加载不同URL时出现内存不足错误,但我相同的代码在Asha501中运行良好。
我必须做什么??任何人都可以帮助我。 我已添加用于 Web 响应的数据检索代码
public static String getDataFromServer(String serverUrl) {
HttpsConnection httpConn = null;
InputStream is = null;
String dataRead = "";
try {
httpConn = (HttpsConnection) Connector.open(serverUrl);
if ((httpConn.getResponseCode() == HttpsConnection.HTTP_OK)) {
int length = (int) httpConn.getLength();
is = httpConn.openInputStream();
if (length == -1) {
int chunkSize = 1023;
byte[] data = new byte[chunkSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int dataSizeRead;
while ((dataSizeRead = is.read(data)) != -1) {
baos.write(data, 0, dataSizeRead);
}
dataRead = new String(baos.toByteArray());
baos.close();
} else {
DataInputStream dis = new DataInputStream(is);
byte[] data = new byte[length];
dis.readFully(data);
dataRead = new String(data);
}
} else {
dataRead = SERVER_ERROR;
}
} catch (Throwable t) {
dataRead = NO_CONNECTION;
} finally {
try {
if (is != null) {
is.close();
}
} catch (Throwable t) {
}
try {
if (httpConn != null) {
httpConn.close();
}
} catch (Throwable t) {
}
}
return dataRead;
}
你会用你得到的数据做什么?
如果要解析该数据,则可以返回InputStream而不是String。
如果要将其保存到文件中,请将 OutputStream 参数添加到该方法中。