0

Getting out of of memory error while loading different URLs in Nokia s40 Asha 305 device, but my same code works well in Asha501..

What i have to do?? Any one can help me. i have added my data retrieve code for web response

    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;
}
Joseph
  • 131
  • 10

1 Answers1

0

What will you do with the data you got?
If you are going to parse that data you could return InputStream instead of String.
If you are going to save it to a file add an OutputStream parameter to the method.

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22