Retrofit上传文件到oss文件存储

复制代码
@Override
public Observable<BaseResp<UploadFileInfoResp>> getUploadFileUrl(UploadFileInfoReq uploadFileInfoReq) {
    return apiService.getUploadFileUrl(uploadFileInfoReq);
}
复制代码
@Override
public Observable<ResponseBody> uploadFileDirect(String url, Map<String, String> headers, RequestBody requestBody) {
    return apiService.uploadFileDirect(url, headers, requestBody);
}
复制代码
public class UploadFileInfoReq {
    public String fileName;
    public long fileSize;
    public boolean randomFileName;
    public String systemCode;
}
复制代码
public class UploadFileInfoResp {
    public long fileId;
    public String fileUrl;
    public String uploadUrl;
    public Map<String, String> headers;
}
复制代码
public void getUploadFileUrl(File file, UploadFileInfoReq uploadFileInfoReq) {
    dataRepository.getUploadFileUrl(uploadFileInfoReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(this)
            .subscribe(new DisposableObserver<BaseResp<UploadFileInfoResp>>() {
                @Override
                public void onNext(@NonNull BaseResp<UploadFileInfoResp> response) {
                    if (response.code == BaseConstants.API_HANDLE_SUCCESS) {
                        uploadFileDirect(file, response.data);
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
}
复制代码
public void uploadFileDirect(File file, UploadFileInfoResp uploadFileInfoResp) {

    RequestBody requestBody = RequestBody.create(file, MediaType.parse("image/png"));

    dataRepository.uploadFileDirect(uploadFileInfoResp.uploadUrl, uploadFileInfoResp.headers, requestBody)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(this)
            .subscribe(new DisposableObserver<ResponseBody>() {
                @Override
                public void onNext(@NonNull ResponseBody response) {
                    Log.i("xxx", "------ done-");
                }

                @Override
                public void onError(@NonNull Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

}
复制代码
topBarView.setOnSettingsClickListener(new TopBarView.OnSettingsItemClickListener() {
    @Override
    public void onClick(View view) {
        File file = new File(getExternalCacheDir().getAbsolutePath() + "/xxx.png");
        showToast("size=" + file.length());
        UploadFileInfoReq uploadFileInfoReq = new UploadFileInfoReq("TestFile", file.length(), "Test_File_001");
        viewModel.getUploadFileUrl(file, uploadFileInfoReq);
    }
});
相关推荐
老码沉思录3 天前
Android开发实战班 -网络编程 - Retrofit 网络请求 + OkHttp 使用详解
android·网络·retrofit
粤M温同学8 天前
Android 使用Retrofit 以纯二进制文件流上传文件
android·retrofit
一名技术极客13 天前
不好意思,HttpClient 该换了,来感受下Retrofit的魅力
java·retrofit
帅次2 个月前
解决 Android WebView 无法加载 H5 页面常见问题的实用指南
android·okhttp·gradle·binder·webview·retrofit·appcompat
追梦的鱼儿3 个月前
okhttp,retrofit,rxjava 是如何配合工作的 作用分别是什么
okhttp·rxjava·retrofit
愿相知3 个月前
【Android】最好用的网络库:Retrofit
网络·retrofit
要小鑫呐3 个月前
实际项目中,运用Retrofit和OkHttp调用其他项目接口
java·网络·okhttp·retrofit
江南一舟1104 个月前
TabLayout使用以及自定义tab标签
android·kotlin·retrofit
骑猪追大象5 个月前
使用Retrofit2+OkHttp监听上传或者下载进度会执行两次的问题
okhttp·retrofit·监听上传下载进度
Android_Trot6 个月前
鸿蒙开发HarmonyOS Next 网络框架retrofit 封装 viemodel使用
harmonyos·鸿蒙·retrofit