我从服务器下载文件。但是当我想获取文件大小时总是 body.contentLength()
返回 -1 。有一些方法可以解决这个问题,HttpUrlConnection
但是ResponseBody
类呢?
private void initDownload(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://mobleh.com/").build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
Call<ResponseBody> request = requestInterface.getAPK(path);
try {
downloadFile(request.execute().body());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
private void downloadFile(ResponseBody body) throws IOException {
int count;
byte data[] = new byte[1024 * 4];
long fileSize = body.contentLength();
}