2011-10-19 7 views
0

私は本当にいくつかの洞察に奇妙な問題があります。私は様々なファイルをダウンロードするいくつかのコードを持っています。いくつかのファイルは、 "chunked"の値を持つ "transfer-encoding"ヘッダーを返します。これが私の問題の原因になっていると私は確信しています。androidに転送エンコーディングヘッダーを含むファイルをダウンロードする

ここは本当に奇妙なところです。私のMotorolla Droidを3gでダウンロードすると、このエンコーディングのファイルは正しくダウンロードされません。すべてのバイトが読み込まれる前にダウンロードが完了するため、破損したファイルがあります。しかし、同じ電話でWiFiを使用すると、同じファイルがうまくダウンロードされます。

これはサーバーの問題のようですか?アンドロイドの問題? 3g問題?私のコードの問題?ここで

は、私はレスポンスヘッダを読み取るために使用しているいくつかのコードです:

private void readResponseHeaders(HttpResponse response) throws StopRequest { 
     Header header = response.getFirstHeader("Content-Disposition"); 
     if (header != null) { 
      mInfo.mInnerState.mHeaderContentDisposition = header.getValue(); 
     } 
     header = response.getFirstHeader("Content-Location"); 
     if (header != null) { 
      mInfo.mInnerState.mHeaderContentLocation = header.getValue(); 
     } 
     if (mInfo.mMimeType == null) { 
      header = response.getFirstHeader("Content-Type"); 
      if (header != null) { 
       mInfo.mMimeType = sanitizeMimeType(header.getValue()); 
      } 
     } 
     header = response.getFirstHeader("ETag"); 
     if (header != null) { 
      mInfo.mInnerState.mHeaderETag = header.getValue(); 
     } 
     String headerTransferEncoding = null; 
     header = response.getFirstHeader("Transfer-Encoding"); 
     if (header != null) { 
      headerTransferEncoding = header.getValue(); 
     } 
     if (headerTransferEncoding == null) { 
      header = response.getFirstHeader("Content-Length"); 
      if (header != null) { 
       mInfo.mInnerState.mHeaderContentLength = header.getValue(); 
       mInfo.mTotalBytes = Long.parseLong(mInfo.mInnerState.mHeaderContentLength); 

       if (!mFileManager.hasSufficientSpace(mInfo.mTotalBytes, mBasePath)){ 
        throw new StopRequest(ServiceDownloadConstants.STATUS_INSUFFICIENT_SPACE_ERROR, "Not Enough Space"); 
       } 
      } 
     } else { 
      // Ignore content-length with transfer-encoding - 2616 4.4 3 
      if (ServiceDownloadConstants.LOGVV) { 
       Log.v(ServiceDownloadConstants.TAG, 
         "ignoring content-length because of xfer-encoding"); 
      } 
     } 
     if (ServiceDownloadConstants.LOGVV) { 
      Log.v(ServiceDownloadConstants.TAG, "Content-Disposition: " + mInfo.mInnerState.mHeaderContentDisposition); 
      Log.v(ServiceDownloadConstants.TAG, "Content-Length: " + mInfo.mInnerState.mHeaderContentLength); 
      Log.v(ServiceDownloadConstants.TAG, "Content-Location: " + mInfo.mInnerState.mHeaderContentLocation); 
      Log.v(ServiceDownloadConstants.TAG, "Content-Type: " + mInfo.mMimeType); 
      Log.v(ServiceDownloadConstants.TAG, "ETag: " + mInfo.mInnerState.mHeaderETag); 
      Log.v(ServiceDownloadConstants.TAG, "Transfer-Encoding: " + headerTransferEncoding); 
     } 

     boolean noSizeInfo = mInfo.mInnerState.mHeaderContentLength == null 
       && (headerTransferEncoding == null 
        || !headerTransferEncoding.equalsIgnoreCase("chunked")); 
     if (!mInfo.mNoIntegrity && noSizeInfo) { 
      throw new StopRequest(ServiceDownloadConstants.STATUS_HTTP_DATA_ERROR, 
      "can't know size of download, giving up"); 
     } 
    } 

ここで私はデータを転送するために使用しているいくつかのコードです:

private void transferData(byte[] data, InputStream entityStream) throws StopRequest { 
     for (;;) { 
      int bytesRead = readFromResponse(data, entityStream); 
      if (bytesRead == -1) { 
       // success, end of stream already reached 
       handleEndOfStream(); 
       return; 
      } 

      writeDataToDestination(data, bytesRead); 
      mInfo.mInnerState.mBytesSoFar += bytesRead; 

      /** Update SHA1 if needed **/ 
      if(mInfo.mEnclosure.getHash() != null && mSha1 != null){ 
       mSha1.update(data, 0, bytesRead); 
      } 

      if (ServiceDownloadConstants.LOGVV) { 
       Log.v(ServiceDownloadConstants.TAG, "downloaded " + mInfo.mInnerState.mBytesSoFar + " for " 
        + mInfo.mEnclosure.getUrl()); 
      } 

      checkPausedOrCanceled(); 
     } 
    } 

編集 さらに、デバッグ時には、私は "転送エンコーディング"ヘッダーが送信されていないときでも、まったく同じ結果を受け取っています。したがって、問題はWifi上で動作し、3gでは動作しないという一般的な問題のようです。

答えて

0

3Gプロバイダはキャッシュファイルを作成し、キャッシュが壊れることがあります。リクエストにno-cacheヘッダーを設定してみてください。

+0

ありがとう、私はこれをできるだけ早く試みます。しかし、私はなぜ私がこの問題を抱えているのかについて真の理由を見つけたと信じています。私はここに私の問題が反映されていると思う新しい質問を投稿しました。 http://stackoverflow.com/questions/7824387/why-does-my-mobile-carrier-re-encode-a-file-while-i-download-it – Pzanno

関連する問題