Я делаю запрос http Get. Мне нужно получать огромные данные, но получая OutOfMemory Exception при чтении данных.OutOfMemory Ошибка при чтении данных из http Получить запрос android
Мой код:
public static String getData(String url) throws CustomException {
// http post
InputStream is = null;
StringBuilder sb = null;
String result = null;
HttpGet httppost;
HttpEntity entity;
try {
HttpClient httpclient = new DefaultHttpClient();
httppost = new HttpGet(url);
HttpResponse response = httpclient.execute(httppost);
entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
throw new CustomException("Could not establish network connection");
}
// convert response to string
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
byte buffer[] = new byte[1024];
while ((c = is.read(buffer)) > -1)
baos.write(buffer, 0, c); //**OutOfMemory Exception.**
byte[] data = baos.toByteArray();
is.close();
result = new String(data, 0, data.length, "utf-8");
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
throw new CustomException("Error parsing the response");
}
return result;
}
URL, который я прохожу к GetData является: http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist09122013014749.txt
Пожалуйста, предложите мне, как решить эту проблему.
Есть так много записей thats почему .. Я думаю, вы должны установить приложение в SDCard или на телефоне. – Riser
Чувак, URL-адрес возвращает много данных. Не добавляйте его в память. Используйте файловую систему. –