2011-10-24 4 views
4

웹 서버에 연결되어 응답하는 응용 프로그램을 만들고 있습니다. 이 응답을 장치의 내부 메모리에있는 txt 파일에 저장합니다. 이제 전체 파일을 한 줄씩 읽어야합니다. 왜냐하면 전체 파일을 읽으려고하면 OutofMemoryException을 던지기 때문입니다.Android에서 큰 파일을 한 줄씩 읽습니다.

httpclient = new DefaultHttpClient(); 
    httppost = new HttpPost("http://www.rpc.probnata.com"); 


    postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("debug_data","1")); 


    httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

    HttpResponse response = httpclient.execute(httppost); 
    Log.w("Response ","Status line : "+ response.getStatusLine().toString()); 
    buffer = EntityUtils.toByteArray(response.getEntity()); 
    FileOutputStream out = this.openFileOutput("response",this.MODE_PRIVATE); 
    out.write(buffer); 
    out.close(); 

파일 읽기 : 지금은 작성하고이 코드를 사용하여 파일을 읽고 있어요 이처럼

public void parseResponse(){ 
    try { 
     File myDir = new File(getFilesDir().getAbsolutePath()); 

     BufferedReader br = new BufferedReader(new FileReader(myDir + "/response")); 
     String line; 
     while ((line = br.readLine()) != null) { 
      Log.v("","line : "+line); 
      handleDataFromSync(line); 
     } 
     br.close(); 



    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

이 메소드는 응답을 구문 분석합니다.

public void handleDataFromSync(final String responseBody) { 

     for(int index=0;index<responseBody.length();index++){ 
       Log.w("Response ","Response size : "+ responseBody.length()); 
       Log.w("","****************Index is : "+index); 

       int objectIdentificator = 0; 
       objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0])); 
       Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator); 
       index = index+packetFieldSizes[0]; 
       Log.w("","****************Index is (must be 32) : "+index); 

       String type = null; 
       type = responseBody.substring(index,index + packetFieldSizes[1]); 
       Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ type); 
       short pType = Short.parseShort(type); 
       Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType); 

       index = index + packetFieldSizes[1]; 
       Log.w("","****************Index is (must be 35) : "+index); 

       String operation=null; 
       operation = responseBody.substring(index,index + packetFieldSizes[2]); 
       short operationType = Short.parseShort(operation); 
       Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operation); 
       Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType); 
       index = index + packetFieldSizes[2]; 
       Log.w("","****************Index is (must be 38) : "+index); 

       String objectId=null; 
       objectId = responseBody.substring(index, index + packetFieldSizes[3]); 
       Log.w("Response ","UID (CHAR, length 32) : "+ objectId); 
       index = index + packetFieldSizes[3]; 
       Log.w("","****************Index is (must be 70) : "+index); 

       int id=0; 
       id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4])); 
       Log.w("Response ","ID (LONGINT) : "+ responseBody.substring(index, index + packetFieldSizes[4])); 
       Log.w("Response ","ID (LONGINT) : "+ id); 
       index = index + packetFieldSizes[4]; 
       Log.w("","****************Index is (must be 102) : "+index); 

       String size=null; 
       size = responseBody.substring(index,index + packetFieldSizes[5]); 
       int dataSize = Integer.parseInt(size); 
       Log.w("Response ","Data Size (LONGINT) : "+ dataSize); 
       index = index + packetFieldSizes[5]; 
       Log.w("","****************Index is (must be 134) : "+index); 

       String hash=null; 
       hash = responseBody.substring(index,index + packetFieldSizes[6]); 
       Log.w("Response ","Data Hash (CHAR, length 32 : "+ hash); 
       index = index + packetFieldSizes[6]; 
       Log.w("","****************Index is (must be 166) : "+index); 

       String dType=null; 
       dType = responseBody.substring(index,index + packetFieldSizes[7]); 
       Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dType); 
       short dataType = Short.parseShort(dType); 
       Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType); 
       index = index + packetFieldSizes[7]; 
       Log.w("","****************Index is (must be 169) : "+index); 

       String data=null; 
       data = responseBody.substring(index, index + dataSize); 
       Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data); 

       index = (index + dataSize)-1; 
       Log.w("","****************Index is must be : "+index); 
       byte[] first = Base64.decode(data); 
       String string = new String(first, "UTF-8"); 
       Log.w("Response ","BASE 64 : "+ string); 
     } 
} 

그래서이 코드가 지금하고있는 일이 첫 번째 줄을 읽을 수 있고 그 후 다시 첫 번째 줄을 읽는 tryint 때문에, 다음 라인을 읽는 방법에 어떤 생각.

답변

2

당신의 bufferReader 구현은 괜찮아 보이므로 내 의심은 당신의 handleDataFromSync 메소드에서 계속 될 것입니다. 코드에서 각 responseBody는 불확정 한 길이의 문자열이지만 알려진 위치에서 값의 패턴을 반복하는 것으로 추측합니다. 잠재적 인 범인은 우리가 조사 할 수없는 packetFieldSizes []와 dataSize의 값일 수 있습니다.

인덱스 로깅 값이 예상 한 값이고 실제로 handleDataFromSync에서 벗어 났는지 확인하십시오. 그럴 경우, 내가 보지 못했던 bufferedReader에 문제가 있으며, 그 단계에서 오류의 원인을 파악하기 위해 추가 로깅을 추가해야합니다.

+0

실제로 파일을 읽는 중 문제가 있음을 발견했습니다. 내 응답이 30K 미만이면 30K에 도달하는 응답이 끝날 때 일부 문자가 추가되어 예외가 발생합니다. 답변 해주셔서 감사합니다! 나는 당신이 나의 질문에 대답하는 유일한 사람이기 때문에 받아 들인 것으로 표시한다.) 고마워! –

+0

다행이라고 생각하면 다행이다! – Carth

관련 문제