在安卓系统中使用OkHttp3上传文件时显示ProgressBar,可以通过以下步骤实现:
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0" />
import okhttp3.*;
import java.io.File;
import java.io.IOException;
public class FileUploader {
private static final String UPLOAD_URL = "http://example.com/upload"; // 替换为实际的上传URL
public void uploadFile(File file, final ProgressListener progressListener, final Callback callback) {
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
})
.build();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
Request request = new Request.Builder()
.url(UPLOAD_URL)
.post(requestBody)
.build();
client.newCall(request).enqueue(callback);
}
public interface ProgressListener {
void onProgress(int progress);
}
private static class ProgressResponseBody extends ResponseBody {
private final ResponseBody responseBody;
private final ProgressListener progressListener;
private BufferedSource bufferedSource;
public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
this.responseBody = responseBody;
this.progressListener = progressListener;
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long totalBytesRead = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
int progress = (int) (totalBytesRead * 100 / responseBody.contentLength());
progressListener.onProgress(progress);
return bytesRead;
}
};
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import java.io.File;
import java.io.IOException;
public class MainActivity extends Activity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
File file = new File("path/to/file"); // 替换为实际的文件路径
FileUploader fileUploader = new FileUploader();
fileUploader.uploadFile(file, new FileUploader.ProgressListener() {
@Override
public void onProgress(final int progress) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
}
});
}
}, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 处理上传失败的情况
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 处理上传成功的情况
}
});
}
}
这样,当文件上传时,ProgressBar将显示上传进度。你可以根据实际需求进行进一步的定制和优化。
腾讯云相关产品推荐:腾讯云对象存储(COS),它是一种安全、稳定、高效、低成本的云端存储服务,适用于存储和处理各类文件,包括图片、音视频、文档等。你可以通过以下链接了解更多信息:
领取专属 10元无门槛券
手把手带您无忧上云