在诺基亚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;
}
答案 0 :(得分:0)
您将如何处理所获得的数据? 如果要解析该数据,则可以返回InputStream而不是String 如果要将其保存到文件中,请将OutputStream参数添加到方法中。