0

在诺基亚 s40 Asha 305 设备中加载不同 URL 时出现内存不足错误,但我的相同代码在 Asha501 中运行良好。

我该怎么办??任何人都可以帮助我。我已经为网络响应添加了我的数据检索代码

    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;
}
4

1 回答 1

0

你将如何处理你得到的数据?
如果要解析该数据,则可以返回 InputStream 而不是 String。
如果要将其保存到文件中,请在方法中添加一个 OutputStream 参数。

于 2013-10-03T17:25:18.980 回答