Retrofit Upload multiple files and parameters
Retrofit 的介绍以及基本使用 这里不再说明。
关于多文件上传 以及上传文件的同时携带多个参数说明
网上涉及到的不是太多。
上一张帅图:
代码:
apiService:
/**
params 参数 files 文件
*/
@Multipart
@POST
Observable<String> uploadFile(@Url String url, @Part List<MultipartBody.Part > files,@PartMap Map<String, RequestBody> params);
ApiInteractor:
Subscription uploadFile(String url, List<MultipartBody.Part > files, Map<String, RequestBody> param, BaseSubscribe<String> subsribe);
ApiInteractorImpl:
/**
* 文件上传
* @param url
* @param param
* @param subsribe
* @return
*/
@Override
public Subscription uploadFile(String url, List<MultipartBody.Part > files, Map<String, RequestBody> param, BaseSubscribe<String> subsribe) {
Observable<String> uploadFile = apiService.uploadFile(url,files,param);
Subscription subscription = uploadFile.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(new RetryWithDelay(App.getContext(), startTimeOut, maxTimeOut, SECONDS))
.onErrorReturn(throwable -> null)
.subscribe(subsribe);
return subscription;
} Activity 中代码:
rivate void upload() {
File file2 = new File(cacheVideo + "girl.jpg");
Map<String,RequestBody> map = new HashMap<>();//参数
List<MultipartBody.Part> mList = new ArrayList<>();//文件 map.put("后台需要的字段key",createPartFromString("你的参数值"));
mList.add(prepareFilePart("resource",file2));
Subscription uploadFile = api.uploadFile(ConstantApi.uploadMp4AAC,mList,map, new BaseSubscribe<String>() {
@Override
public void onSuccess(String result) {
Log.d("上传返回结果是", "onSuccess: "+result);
}
});
subscription.add(uploadFile);
}
public static final String MULTIPART_FORM_DATA = "multipart/form-data"; private RequestBody createPartFromString(String descriptionString) {
return RequestBody.create(
MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
} private MultipartBody.Part prepareFilePart(String partName, File fileName) {
RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA),fileName);
UploadRequestBody uploadRequestBody=new UploadRequestBody(requestFile, new UploadRequestBody.Listener() {
@Override
public void onRequestProgress(long bytesWritten, long contentLength) {
Log.e("上传进度","file1:"+contentLength+":"+bytesWritten);
}
});
return MultipartBody.Part.createFormData(partName, fileName.getName(), uploadRequestBody);
} 重写RequestBody 监听回调
/**
* 文件上传
* Created by swplzj on 17/2/17.
*/ public class UploadRequestBody extends RequestBody { protected RequestBody delegate;
protected Listener listener; protected CountingSink countingSink; public UploadRequestBody(RequestBody delegate, Listener listener) {
this.delegate = delegate;
this.listener = listener;
} @Override
public MediaType contentType() {
return delegate.contentType();
} @Override
public long contentLength() {
try {
return delegate.contentLength();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
} @Override
public void writeTo(BufferedSink sink) throws IOException { countingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(countingSink); delegate.writeTo(bufferedSink); bufferedSink.flush();
} protected final class CountingSink extends ForwardingSink { private long bytesWritten = 0; public CountingSink(Sink delegate) {
super(delegate);
} @Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount); bytesWritten += byteCount;
listener.onRequestProgress(bytesWritten, contentLength());
} } public interface Listener {
void onRequestProgress(long bytesWritten, long contentLength);
}
}
// ==================至此 基本结束 Retrofit 同时上传多个参数 和 文件结束
主要是 @后面的关键字段我们可能不是太清楚 或者很少用到
参考:
https://square.github.io/retrofit/
https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server欢迎交流指正学习~
群 521039620 群主很热心 很谦逊 有问题 找晨希哥~
最后: 最近看到应用商店有配音的软件 X配音 配音功能做的还不错 反编译源码 看到 他是将 用FFmpeg 从pcm入手 和video合成。关于 Pcm 合成 以及指定位置插入 目前还不清楚。 后面我自己尝试了一下 用切割的方法。大致实现了 配音 跟读 混合 合成
但是感觉切割 还是不太好。 可能表达的不太清楚。后面解决问题之后 会记录一下 FFmpeg的使用。主要使用到 //音频合成 compile 'com.googlecode.mp4parser:isoparser:1.1.21'
//音频资源转换格式
compile 'com.github.adrielcafe:AndroidAudioConverter:0.0.8' 其中AndroidAudioConverter底层是基于 FFmpeg的封装。后面再说吧~
Retrofit Upload multiple files and parameters的更多相关文章
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- How to Upload multiple files to documentLibrary in one time
In a Sharepoint 2013 website,we can upload one file to the documentlibrary by click "Uploa ...
- SharePoint 2013 Step by Step—— How to Upload Multiple Documents in Document Library
How to Upload Multiple documents in SharePoint 2013,Options to add multiple files in a document libr ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
- How to effectively work with multiple files in Vim?
Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe ...
- Exception: Operation xx of contract xx specifies multiple request body parameters to be serialized without any wrapper elements.
Operation 'CreateProductCodeStock' of contract 'IChileService' specifies multiple request body param ...
- Uploading multiple files asynchronously by blueimp jquery-fileupload
Uploading multiple files asynchronously by blueimp jquery-fileupload Solved. Fiddle: http://jsfidd ...
- How to attach multiple files in the Send Mail Task in SSIS
Let’s say you need to create a SSIS package that creates 2 files and emails the files to someone. Yo ...
- jQuery file upload --Multiple File Input Fields in One Form
The plugin can be applied to a form with multiple file input fields out of the box. The files are se ...
随机推荐
- 无节操cocos2d-js游戏
1. <看谁抽得快> 2. <拍苍蝇> 3. <月饼达人> 4. <亲吻小游戏> 下面这些是本人做的,需要源代码的可以回复我 ps:全部采 ...
- EventStore文件存储设计
背景 ENode是一个CQRS+Event Sourcing架构的开发框架,Event Sourcing需要持久化事件,事件可以持久化在DB,但是DB由于面向的是CRUD场景,是针对数据会不断修改或删 ...
- kbmMW实现sql查询(图文并茂)
kbmMW对于Delphi来说,是最好的多层框架,没有之一,无论是效率.稳定及架构都让人无可挑剔,尤其自Delphi支持跨平台开发以来,随着Delphi支持ios及Android移动开发,KbmMW也 ...
- vue文档重读有感
vue 官方文档,每次读都有不一样的感受.项目已经做过一个了,遇到了不少问题,下面总结下这次看到的注意点: 一.指令方面 1. v-once 一次性绑定,只渲染元素和组件一次.随后的重新渲染,元素/ ...
- hdu 1400 Mondriaan's Dream 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1400 题目意思:给出一个h * w的 大 矩形,需要用 1 * 2 的砖块去填充这个大矩形,问填充的方 ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- python中的编码和解码
计算机中常见的编码方式有多种,英文一般是ascii编码,其他有unicode,utf-8,gbk,utf-16等编码. 常见编码方式: ASCII编码:ASCII是早期的编码,包含英文字母.数字和 ...
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description 给定N个数对(xi, yi),求最长上升子 ...
- [POI 2014] Little Bird
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3831 [算法] 单调队列优化动态规划 时间复杂度 : O(N) [代码] #incl ...
- bzoj3143游走——期望+高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3143 只需算出每条边被经过的概率,将概率从小到大排序,从大到小编号,就可得到最小期望: 每条 ...