2014-02-21 51 views
1
@Override 
    public void run() { 
     URL imgurl; 
     int Read; 
     try { 
      imgurl = new URL(ServerUrl); 
      HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection(); 
      int len = conn.getContentLength(); 
      Log.d("check", "ContentLength:" + len); 
      Log.d("check", "ServerUrl:" + ServerUrl); 
      Log.d("check", "LocalPath:" + LocalPath); 
      byte[] tmpByte = new byte[len]; 
      InputStream is = conn.getInputStream(); 
      File file = new File(LocalPath); 
      FileOutputStream fos = new FileOutputStream(file); 
      for (;;) { 
       Read = is.read(tmpByte); 
       if (Read <= 0) { 
        break; 
       } 
       fos.write(tmpByte, 0, Read); 
      } 
      is.close(); 
      fos.flush(); 
      fos.close(); 
      conn.disconnect(); 


     } catch (MalformedURLException e) { 
      ut.CalltoAlertDialog_ok(getString(R.string.alert), getString(R.string.setting_skin_downloadfail)); 
     } catch (IOException e) { 
      ut.CalltoAlertDialog_ok(getString(R.string.alert), getString(R.string.setting_skin_downloadfail)); 
     } 

     mAfterDown.sendEmptyMessage(0); 
    } 

這是文件下載源。我的文件下載器突然出現錯誤

此代碼打印錯誤 「NegativeArraySizeException」 從這裏

byte[] tmpByte = new byte[len]; 

所以,我檢查LEN的值。
len的值是-1。

但是.. 當我昨天創建,此代碼不是打印錯誤。我有2個apk文件。我有2個apk文件。我有2個共享文件對話框。
昨天創建的apk不是問題。即使現在這個apk也沒問題。
但是,今天創建的apk是問題。

我沒有修改任何東西。

這是什麼原因?

+1

變量應該用小寫字母開頭。否則混淆。 –

+0

如果代碼工作(代碼相同),那麼你的問題似乎是在服務器端。 – Merlevede

回答

1

我覺得你的問題是在這裏:

有關 getContentLength方法

HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection(); 
    int len = conn.getContentLength(); 

閱讀文檔返回響應頭 字段內容長度或-1,如果指定字節的內容長度此字段未設置。

返回響應頭字段content-length的值。

所以這個getContentLength返回-1的情況似乎發生在你身上。然後你用這個-1來設置你的數組大小。 =>拋出異常

檢查此解決方案question about getContentLength returning -1,也許你將不得不做類似的事情。

但至少你必須檢查len > 0設置你的數組大小之前