我正在處理android上的http get request。ArrayList中的OutOfMemory錯誤。 Android
我從服務器收到大量的json數據,字符串無法處理或存儲該數據並獲取OutOfMemory異常。
然後我試着將它保存在arrayList中,它可以存儲最大Integer.Maximum值。 ,但它存儲在〜8970位置時正在發生OutOfMemory異常。
這是我的鏈接,它有json數據。
http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist10122013035042.txt
這裏是我的代碼:
ArrayList<String> newarr = new ArrayList<String>();
try {
URL url = new URL(urlFilePath);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
// urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
int check;
while ((bufferLength = inputStream.read(buffer)) > 0) {
String decoded = new String(buffer, 0, bufferLength);
newarr.add(decoded); // OutOfMemory Exception.
}
fileOutput.close();
buffer = null;
inputStream.close();
String path = file.getAbsolutePath();
return path;
} catch (final MalformedURLException e) {
e.printStackTrace();
return "";
} catch (final IOException e) {
e.printStackTrace();
return "";
} catch (final Exception e) {
System.out.println("EXCEPTION:: " + e.getMessage());
e.printStackTrace();
return "";
}
嘗試使用'StringBuilder'而不是'String'並檢查是否有幫助。 – Apoorv
感謝您的回覆。我剛剛嘗試過,但沒有奏效。得到相同的錯誤。 – user2085965
嘗試評論'newarr.add(解碼);'看看它是否仍然崩潰。 – Apoorv