2012-03-08 2 views
2

문제가 있습니다. 파일의 길이가 -1입니다. 나는 그것을 고치는 많은 시간을 보냈다. 나는 이것이 왜 일어나는 지 모른다. 또한 asynctask를 다시 호출 할 때 song1은 동일하게 유지됩니다 (하지만 song1 값은 아무런 문제가 없습니다. 축배로 테스트 한 결과 작동합니다). 'song1'은 문자열이고 매번 다른 값을가집니다. 고맙습니다.파일 길이가 -1로 늘어납니다. asynctask android 다운로드

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 

      mProgressDialog.setMessage("Downloading "+song1+".."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
    } 
} 




protected String doInBackground(String... aurl) { 
     int count; 

     try { 
      URL url = new URL(aurl[0]); 
      URLConnection conexion = url.openConnection(); 
      ((HttpURLConnection) conexion).setInstanceFollowRedirects(true); 
      conexion.connect(); 


      int lenghtOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/download/"+song1); 

      byte data[] = new byte[20480]; 

      long total = 0; 


      while ((count = input.read(data)) != -1) { 
       total += count; 
       publishProgress(""+(int)((total/lenghtOfFile)*100)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) {} 
     return null; 

    } 

여기 -1의 길이가 드물지 않다 얻기 내 로그 캣

03-08 20:25:17.714: D/ANDRO_ASYNC(14559): Lenght of file: -1 
03-08 20:25:18.564: D/dalvikvm(14559): GC freed 18632 objects/1179128 bytes in 112ms 
03-08 20:25:18.864: I/global(14559): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required. 
03-08 20:25:18.874: D/ANDRO_ASYNC(14559): -420000 
03-08 20:25:18.954: D/ANDRO_ASYNC(14559): -700000 
03-08 20:25:18.984: D/ANDRO_ASYNC(14559): -1820000 
03-08 20:25:19.029: D/ANDRO_ASYNC(14559): -1960000 

답변

0

입니다. 일반적으로 서버가 데이터 버퍼를 사용자에게 보냈지 만 (가득 찼기 때문일 수 있음), 응답의 전체 크기를 여전히 알지 못합니다. 또는 게으른 개발자입니다.

길이가 -1 인 경우 스트림 끝까지 정보를로드하면됩니다.

+0

정상적으로 -1 길이로 다운로드하지만 기본 문제는 진행률 막대가 작동하지 않는다는 것입니다. 여전히 0 %에 머물렀고 다운로드가 완료된 후에 사라졌습니다. –

+0

진행률 표시 줄로 onProgressUpdate 메소드에서 아무 것도하고 있습니까? 가치는 무엇입니까? 나는 그것이 매우 큰 음수 값이라고 추측하고 있습니다. 따라서 0에 머무르는 것이 나에게 의미가 있습니다. –

+0

비동기 작업 및 진행에 대한이 질문을 살펴보십시오. http://stackoverflow.com/questions/6450275/android-how-to-work-with-asynctasks-progressdialog –