Skip to content

Commit

Permalink
feat 新增了文件上传的进度监听打印日志,并且控制了频率
Browse files Browse the repository at this point in the history
  • Loading branch information
wuao committed Sep 6, 2023
1 parent 282f212 commit 49f7ff3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void dependsOnTask(ApplicationVariant applicationVariant, boolean isIni
uploadTask.init( isInit?applicationVariant:null, project1);
//依赖关系 。上传依赖打包,打包依赖clean。
if (isInit){
applicationVariant.getAssembleProvider().get().dependsOn(project1.getTasks().findByName("clean"));
// applicationVariant.getAssembleProvider().get().dependsOn(project1.getTasks().findByName("clean"));
uploadTask.dependsOn(applicationVariant.getAssembleProvider().get());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.trubitpro.uploadapkplugin.help;

public interface ProgressListener {
void onProgress(long currentBytes, long totalBytes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.trubitpro.uploadapkplugin.help;

import java.io.IOException;

import javax.annotation.Nullable;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;

public class ProgressRequestBody extends RequestBody {
private RequestBody requestBody;
private ProgressListener progressListener;

public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) {
this.requestBody = requestBody;
this.progressListener = progressListener;
}

@Nullable
@Override
public MediaType contentType() {
return requestBody.contentType();
}

@Override
public long contentLength() throws IOException {
return requestBody.contentLength();
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink bufferedSink = Okio.buffer(new ForwardingSink(sink) {
private long bytesWritten = 0L;
private long contentLength = 0L;

@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
if (contentLength == 0) {
contentLength = contentLength();
}
bytesWritten += byteCount;
progressListener.onProgress(bytesWritten, contentLength);
}
});

requestBody.writeTo(bufferedSink);
bufferedSink.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.trubitpro.uploadapkplugin.entry.PgyCOSTokenResult;
import com.trubitpro.uploadapkplugin.help.CmdHelper;
import com.trubitpro.uploadapkplugin.help.HttpHelper;
import com.trubitpro.uploadapkplugin.help.ProgressListener;
import com.trubitpro.uploadapkplugin.help.ProgressRequestBody;
import com.trubitpro.uploadapkplugin.help.SendMsgHelper;


Expand Down Expand Up @@ -60,9 +62,21 @@ public void uploadPgyQuickWay(File apkFile) {
} catch (UnsupportedEncodingException e1) {
//TODO
}
final int[] progressInit = {0};
ProgressRequestBody progressRequestBody=new ProgressRequestBody(fileBody, new ProgressListener() {
@Override
public void onProgress(long currentBytes, long totalBytes) {
int progress = (int) (currentBytes * 100 / totalBytes);
if (progress==100||progress- progressInit[0] >8){
progressInit[0] =progress;
System.out.println("上传APK进度-------"+progress+"%------------");

}
}
});
MultipartBody mBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file" , name , fileBody)
.addFormDataPart("file" , name , progressRequestBody)
.build();
Request request = new Request.Builder()
.url("https://test-api.trubit.com/member-api/api/v1/uploadApp")
Expand Down

0 comments on commit 49f7ff3

Please sign in to comment.