Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从HttpResponse获取文件大小_Android_Http_Post_Progressdialog - Fatal编程技术网

Android 如何从HttpResponse获取文件大小

Android 如何从HttpResponse获取文件大小,android,http,post,progressdialog,Android,Http,Post,Progressdialog,我需要在从服务器加载文件时显示progressbar,但我不知道如何获取文件大小。我使用这样的POST方法 HttpPost httppost = new HttpPost(url); Inputstream toDown = null; List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.a

我需要在从服务器加载文件时显示progressbar,但我不知道如何获取文件大小。我使用这样的POST方法

        HttpPost httppost = new HttpPost(url);
        Inputstream toDown = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("p1", p1));
        nameValuePairs.add(new BasicNameValuePair("p2", p2));
        nameValuePairs.add(new BasicNameValuePair("p3", p3));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        //Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        if(response.getStatusLine().getStatusCode()!=200){
            toDown = null;
        }

        else{
            toDown = response.getEntity().getContent();
        }
HttpPost-HttpPost=新的HttpPost(url);
Inputstream toDown=null;
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“p1”,p1));
添加(新的BasicNameValuePair(“p2”,p2));
添加(新的BasicNameValuePair(“p3”,p3));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
如果(response.getStatusLine().getStatusCode()!=200){
toDown=null;
}
否则{
toDown=response.getEntity().getContent();
}

但我不知道如何处理这些信息来获得文件大小

并非所有服务器都支持头文件。以下powershell代码在不下载文件本身的情况下获取文件大小:

$param1 = 0
$param2 = 0
$param3 = "https://URI"

# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http

# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient
$client.DefaultRequestHeaders.Range = New-Object -TypeName System.Net.Http.Headers.RangeHeaderValue($param1, $param2)

$HttpMethod = New-Object -TypeName System.Net.Http.HttpMethod("get")
$HttpRequestMessage = New-Object -TypeName System.Net.Http.HttpRequestMessage($HttpMethod, $param3) 
$HttpResponseMessage = $client.SendAsync($HttpRequestMessage)
$HttpResponseMessage.wait();

$size = $HttpResponseMessage.result.content.headers.contentrange.length
echo $size

在Powershell 5.1.17134.1上测试,Win 10

服务器可能会通过响应标题告知“内容长度”,但当我使用response.getEntity().getContentLength()时,如果您的服务器没有告知您内容长度,我只会得到-1.0。