如何使用翻新2获得文件大小。我尝试过返回-1?

问题描述 投票:0回答:1

我想构建App以使用进度条下载文件,我试图从服务器获取文件大小。使用翻新2库时,但无法获得文件总大小。并返回-1,我在build:gradle:3.0.1上测试了它的工作,但在build:gradle:3.5.3

上没有测试

这里是我的代码

    ApiService apiService = RetroClient.getApiService();
    Call<ResponseBody> call = apiService.downloadFile("fsharpapps/fonts/customFont.ttf");
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                    if (response.body() != null) {
                        AppUtil.mToast(DownloadService.this, "Downloading...");
                        boolean isDownload = writeResponseBodyToDisk(response.body());
                        if (isDownload) 
                            AppUtil.mToast(DownloadService.this, "file size" + response.body().contentLength());
                      }}
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
        }
    });
// output is file size -1

但是当我检查header文件时,它运行良好。

here is HTTP Status for: "http://mywebsite.com/fsharpapps/fonts/customFont.ttf"

    HTTP/1.1 200 OK
    Date: Tue, 31 Dec 2019 19:03:16 GMT
    Server: Apache
    Upgrade: h2,h2c
    Connection: Upgrade, close
    Last-Modified: Sat, 21 Oct 2017 08:55:53 GMT
    Accept-Ranges: bytes
    Content-Length: 13857268
    Vary: Accept-Encoding,User-Agent
    Content-Type: font/ttf
android retrofit2 okhttp retrofit2.6
1个回答
1
投票

在界面中使用标题

public interface RetrofitInterface {
    @Headers({"Accept-Encoding: *",
              "Content-Type: font/ttf"})
    @Streaming
    @GET("yourfile.ttf")
    Call<ResponseBody> downloadFile();
}
© www.soinside.com 2019 - 2024. All rights reserved.