在片段B中,我有一个提交按钮,用于将图像提交到服务器。单击提交按钮后,它将返回到片段A。我使用Multipart
将图像上传到服务器,并且该进程正在Thread
上运行,因此用户可以在等待图像上传的同时执行其他任务。 / p>
片段B (单击“提交”按钮时)
inner class SimpleThread() :Thread() {
override fun run() {
for (i in newList!!) {
val service = RetrofitFactory.makeRetrofitService()
GlobalScope.launch(Dispatchers.IO) {
val request2 = WebApi.createImage(
activity,File(i)
)
}
}
}
WebApi
suspend fun createImage(
file: File?
): WebApiResponse.Image? {
val image = file?.let {
MultipartBody.Part.createFormData(
"image",
it.getName(),
RequestBody.create(MediaType.parse("image/*"), it)
)
}
return RetrofitFactory.apiCall(context) {
RetrofitFactory.makeRetrofitService().submitImages(
image
)
}
}
RetrofitService
@Multipart
@POST("create_image")
fun submitImages(
@Part image: MultipartBody.Part?
): Deferred<Response<WebApiResponse.Image>>
我能够将图像上传到服务器,但是如何显示片段A上的图像上传进度?
感谢您的帮助。
答案 0 :(得分:0)
因此,经过一番研究,我为您找到了两种方法
1-如果您使用的是RxJava
,建议您遵循此链接中的说明和注意事项
display progress of multipart with retrofit using RxJava
2-如果您不使用RxJava
,则将ProgressResponseBody
添加到改造生成器中,如下所示:
public class ProgressRequestBody extends RequestBody {
private File mFile;
private String mPath;
private UploadCallbacks mListener;
private String content_type;
private static final int DEFAULT_BUFFER_SIZE = 2048;
public interface UploadCallbacks {
void onProgressUpdate(int percentage);
void onError();
void onFinish();
}
为此,您应该使用this answer
答案 1 :(得分:0)
我想您可以使用WorkManaget或IntenService来显示进度。 A sample to send via retrofit2 您可以在服务中启动该过程。该服务可以发送新事件。
答案 2 :(得分:-1)
自定义RequestBody
public class FileProgressRequestBody extends RequestBody {
public interface ProgressListener {
void transferred(int size);
}
private RequestBody mRequestBody;
protected ProgressListener listener;
public FileProgressRequestBody(File file, String contentType, ProgressListener listener) {
this.mRequestBody = RequestBody.create(MediaType.parse(contentType), file);
this.listener = listener;
}
@Override
public long contentLength() throws IOException{
return mRequestBody.contentLength();
}
@Override
public MediaType contentType() {
return mRequestBody.contentType();
}
@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
if (sink instanceof Buffer) {
// Log Interceptor
mRequestBody.writeTo(sink);
return;
}
CountingSink countingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(countingSink);
mRequestBody.writeTo(bufferedSink);
bufferedSink.flush();
}
protected final class CountingSink extends ForwardingSink {
private long bytesWritten = 0;
public CountingSink(Sink delegate) {
super(delegate);
}
@Override
public void write(@NonNull Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
bytesWritten += byteCount;
if (listener != null) {
long length = contentLength();
if (length != 0) {
listener.transferred(MathUtils.clamp((int) (bytesWritten * 100 / length), 0, 100));
}
}
}
}
}
使用MultipartBody.Part.createFormData("image", file.getName(), new FileProgressRequestBody(file, "image/*", youListener));