简体   繁体   English

getContentLength()在某些设备而非其他设备上返回-1

[英]getContentLength() returning -1 on some devices and not others

I'm trying to obtain the size of a file before I download it. 我正在尝试获取文件的大小,然后再下载它。 I use conn.getContentLength(); 我使用conn.getContentLength(); to do this and it works fine on my home computers Android 2.1 Emulator. 为此,它可以在我的家用计算机Android 2.1 Emulator上正常运行。

It however doesn't work once I run my app from my phone (either WiFi or 3G) and it also doesn't work when I run it from my work laptops Android 2.1 Emulator. 但是,一旦我通过手机(WiFi或3G)运行我的应用程序,该功能将无法使用,而当我从工作笔记本电脑的Android 2.1仿真器中运行该应用程序时,该功能也将无法使用。

Does anyone know a workaround for this? 有谁知道解决方法? Is there another way I can obtain the size of the file maybe without using HttpURLConnection . 还有另一种方法可以获取文件的大小,而无需使用HttpURLConnection

This information will not always be available. 此信息并非始终可用。 Usually you will know the length of the file you are downloading. 通常,您会知道要下载的文件的长度。 Depending on the webserver, the protocol, the connection, and the method of downloading, this information may not always be available. 根据Web服务器,协议,连接和下载方法的不同,此信息可能并不总是可用。

You should definitely modify your application so that it can handle this situation. 您绝对应该修改您的应用程序,以便它可以处理这种情况。 I think you will find that different devices using different connection methods will offer different results with this. 我认为您会发现使用不同连接方法的不同设备将为此提供不同的结果。

Use HttpVersion.HTTP_1_0 for your file downloads. 使用HttpVersion.HTTP_1_0进行文件下载。 This prevents the use of "Chunked transfer encoding" 这样可以防止使用“分块传输编码”

See: http://en.wikipedia.org/wiki/Chunked_transfer_encoding 请参阅: http : //en.wikipedia.org/wiki/Chunked_transfer_encoding

For example, overload the constructor so that you can specify which HTTP version: 例如,重载构造函数,以便您可以指定哪个HTTP版本:

public class HTTPrequest
{
    //member variables
    private SchemeRegistry mSchemeRegistry;
    private HttpParams mHttpParams;
    private SingleClientConnManager mSCCmgr;
    private HttpClient mHttpClient;
    private HTTPrequestListener mHTTPrequestListener = null;

    //constants
    private final int TIMEOUT_CONNECTION = 20000;//20sec
    private final int TIMEOUT_SOCKET = 30000;//30sec

    //interface for callbacks
    public interface HTTPrequestListener
    {
        public void downloadProgress(int iPercent);
    }

    /**
     * Creates an HttpClient that uses plain text only.
     * note: Default constructor uses HTTP 1.1
     */
    public HTTPrequest()
    {
        this(HttpVersion.HTTP_1_1);
    }

    /**
     * Creates an HttpClient that uses plain text only.
     * @param httpVersion HTTP Version (0.9, 1.0, 1.1)
     */
    public HTTPrequest(HttpVersion httpVersion)
    {
        //define permitted schemes
        mSchemeRegistry = new SchemeRegistry();
        mSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        //define http parameters
        mHttpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(mHttpParams, TIMEOUT_CONNECTION);
        HttpConnectionParams.setSoTimeout(mHttpParams, TIMEOUT_SOCKET);
        HttpProtocolParams.setVersion(mHttpParams, httpVersion);
        HttpProtocolParams.setContentCharset(mHttpParams, HTTP.UTF_8);

        //tie together the schemes and parameters
        mSCCmgr = new SingleClientConnManager(mHttpParams, mSchemeRegistry);

        //generate a new HttpClient using connection manager and parameters
        mHttpClient = new DefaultHttpClient(mSCCmgr, mHttpParams);
    }

    public void setHTTPrequestListener(HTTPrequestListener httpRequestListener)
    {
        mHTTPrequestListener = httpRequestListener;
    }

    //other methods for POST and GET
}

When you want to do a file download use HTTPrequest httpRequest = new HTTPrequest(HttpVersion.HTTP_1_0); 当您要下载文件时,请使用HTTPrequest httpRequest = new HTTPrequest(HttpVersion.HTTP_1_0); and when you want to do a POST or GET use HTTPrequest httpRequest = new HTTPrequest(); 当您要进行POST或GET时,请使用HTTPrequest httpRequest = new HTTPrequest();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM