使用DavidWebforAndroid获取上传进度

使用DavidWebforAndroid获取上传进度,android,httpurlconnection,Android,Httpurlconnection,我在这个答案中找到了要上传的代码: Webb-Webb=Webb.create(); 响应=韦伯 .post(“http://www.example.com/service") .body(新文件(“camera/myfile.jpg”)) .连接超时(10*1000) .asString(); if(response.issucess()){ 字符串结果=response.getBody(); // ... }否则{ System.out.println(response.getStatusC

我在这个答案中找到了要上传的代码:

Webb-Webb=Webb.create();
响应=韦伯
.post(“http://www.example.com/service")
.body(新文件(“camera/myfile.jpg”))
.连接超时(10*1000)
.asString();
if(response.issucess()){
字符串结果=response.getBody();
// ...
}否则{
System.out.println(response.getStatusCode());
System.out.println(response.getResponseMessage());
System.out.println(response.getErrorBody());
}

但是我如何跟踪上传的进度呢?

正如您看到的多部分上传一样

无法使用此库的功能,因此无法获取上载文件的进度


您需要使用MultiPart将文件上载到服务器。更多

使用Log.d()而不是System.out.printLn()。正如@ssh正确指出的,本机不支持多部分。但对于这个简单的示例,可以实现一个上传进度回调(只需上传一个文件)。如果需要,在Github上创建一个问题,我将实现它。将适用于
.body(File)
,但不适用于
.body(InputStream)
,因为必须事先知道字节数。谢谢@hgoeble。我知道我们的后端需要多部分上传。我将尝试实现你的库的这个ontop。谢谢你提供了教程的链接。有太多的代码要发布在回复中;)
Webb webb = Webb.create();
Response<String> response = webb
        .post("http://www.example.com/service")
        .body(new File("camera/myfile.jpg"))
        .connectTimeout(10 * 1000)
        .asString();

if (response.isSuccess()) {
    String outcome = response.getBody();

    // ...

} else {
    System.out.println(response.getStatusCode());
    System.out.println(response.getResponseMessage());
    System.out.println(response.getErrorBody());
}