2013-12-10 4 views
0

http 요청을하고 있습니다. 거대한 데이터를 받아야하지만 데이터를 읽는 동안 OutOfMemory 예외가 발생합니다.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

방법이 문제를 해결하는 날을 제안하십시오.

+0

거기에 왜 그렇게 많은 레코드가 있습니다. 나는 당신이 sdcard 또는 휴대 전화 스토리지에 응용 프로그램을 설치해야한다고 생각합니다. – Riser

+0

친구, URL이 많은 데이터를 반환합니다. 그것을 메모리에 추가하지 마십시오. 파일 시스템을 사용하십시오. –

답변

2

받은 파일이 크면 ByteArrayOutputStream 대신 파일에 응답을 작성한 다음 파일에서 결과를 구문 분석해야합니다.

그리고 가능한 경우 서버는 큰 파일을 작은 청크로 분할해야합니다.

0

하나의 가능한 솔루션은 서버에서 결과를 가져 오는 과정에서 페이지 매김을 구현하는 것입니다. 그런 다음 점진적으로 데이터를 쓸 수 있습니다.