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);
    }
});
相关推荐
帅次1 个月前
解决 Android WebView 无法加载 H5 页面常见问题的实用指南
android·okhttp·gradle·binder·webview·retrofit·appcompat
追梦的鱼儿2 个月前
okhttp,retrofit,rxjava 是如何配合工作的 作用分别是什么
okhttp·rxjava·retrofit
愿相知2 个月前
【Android】最好用的网络库:Retrofit
网络·retrofit
要小鑫呐2 个月前
实际项目中,运用Retrofit和OkHttp调用其他项目接口
java·网络·okhttp·retrofit
江南一舟1103 个月前
TabLayout使用以及自定义tab标签
android·kotlin·retrofit
骑猪追大象4 个月前
使用Retrofit2+OkHttp监听上传或者下载进度会执行两次的问题
okhttp·retrofit·监听上传下载进度
Android_Trot5 个月前
鸿蒙开发HarmonyOS Next 网络框架retrofit 封装 viemodel使用
harmonyos·鸿蒙·retrofit
FranzLiszt18475 个月前
Harmony os Next——Retrofit网络库的使用和封装
网络·php·retrofit
哈龙_996 个月前
Android Retrofit 封装模版
retrofit
哈龙_996 个月前
Android retrofit使用模板
android·retrofit