我有一个奇怪的问题,我真的会有所了解。我有一些代码下载各种文件。有些文件返回“transfer-encoding”标题,其值为“chunked”,我很确定这是导致我出现问题的原因。
这里真的很奇怪。当我在3g上运行我的Motorolla Droid时,具有此编码的文件无法正确下载,因为它在读取所有字节之前完成下载,因此我有一个损坏的文件。但是,在完全相同的手机上使用WiFi,同样的文件下载就好了。
这听起来像是服务器问题吗? android问题? 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();
}
}
修改 经过进一步的调试,即使没有发送“transfer-encoding”标题,我也收到了完全相同的结果,所以看起来我的问题就像在Wifi上工作一样通用而且它不在3g上。< / p>
答案 0 :(得分:0)
3G提供商缓存文件,缓存有时会被破坏。尝试在您的请求上设置no-cache标头。