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 ...
随机推荐
- gitlab merge过程
基本步骤如下: 以我的分支为例 1.创建本地分支,命令 git checkout -b liuping_develop2.创建好分支后提交到远程 ,命令 git push origin liuping ...
- Record is locked by another user
Oracle修改表中记录时出现record is locked by another user的问题 在操作表时没有commit,导致表被锁,只要执行下面两行语句,就可以了将行锁解锁了. Select ...
- redis12-----redis 与关系型数据库的对比
书和书签系统 create table book ( bookid int, title ) )engine myisam charset utf8; insert into book values ...
- (linux)自旋锁及其衍生锁
自旋锁 毫秒以下. 自旋锁用于多个CPU系统中,在单处理器系统中,自旋锁不起锁的作用,只是禁止或启用内核抢占.在自旋锁忙等待期间,内核抢占机制还是有效的,等待自旋锁释放的线程可能被更高优先级的线程 ...
- git format-patch 用法
git format-patch HEAD^ # git format-patch -s 1bbe3c8c197a35f79bfddaba099270a2e54ea9c7 please replace ...
- poj 1195 Mobile phones 解题报告
题目链接:http://poj.org/problem?id=1195 题目意思:有一部 mobie phone 基站,它的面积被分成一个个小正方形(1 * 1 的大小),所有的小正方形的面积构成了一 ...
- Oracle:通过dbv查看数据文件是否有坏块
我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...
- hdu-5675 ztr loves math(数学)
题目链接: ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 子集枚举好题UVA1354
题目 分析:枚举子集以及关于该子集的补集,然后用子集去暴力构造一颗二叉树,注意左边的最远距离不一定来自于左子树,右边的最远距离也不一定来自于右子树 #include "iostream&qu ...
- TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
报错原因:numpy不能读取CUDA tensor 需要将它转化为 CPU tensor. 所以如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tenso ...