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 ...
随机推荐
- webservice client setTimeOut
一:eclipse生成的client,基于axis client_sub.getOptions().setTimeOutInMilliSeconds(1000*60); client_sub表示一个客 ...
- ubuntu16.04和服务器 caffe 安装
在centos6.X上安装caffe 0.编译安装gcc4.8.5 由于centos6.x中的gcc版本老旧,不支持c++11所以要安装gcc4.8.5,以下是安装教程.参考CentOS 6.4 编译 ...
- ros使用时的注意事项&技巧2
1.查看参数列表 rosparam list 2.查询参数rosparam get parameter_name,如rosparam get /rosdistro 3.设置参数rosparam set ...
- HDU 5651xiaoxin juju needs help
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 1166 敌兵布阵 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目意思:给出 N 个数你,通过对某些数进行更改(或者 + 或者 -),当输入的是 Query ...
- codeforces 673B B. Problems for Round(模拟)
题目链接: B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Eclipse慢慢学会的快捷键
Java编辑器 添加单个import Ctrl+Shift+M Java编辑器 组织多个import Ctrl+Shift+O Ctrl+M切换窗口的大小 Ctrl+D删除当前行 ---------- ...
- PostgreSQL学习之【用户权限管理】说明
背景 最近在学习PostgreSQL,看了用户权限管理文档,涉及到的知识点比较多,顺便写篇文章进行整理并不定时更新,也方便自己后续进行查阅. 说明 注意:创建好用户(角色)之后需要连接的话,还需要修改 ...
- Code-NFine:下来框和复选框
ylbtech-Code-NFine:下来框和复选框 1.返回顶部 1. 1.1 html $("#F_OrganizeId").bindSelect({ url: "/ ...
- 杂项-公司:Aspose
ylbtech-杂项-公司:Aspose Aspose 于2002年3月在澳大利亚悉尼创建,旗下产品覆盖文档.图表.PDF.条码.OCR.CAD.HTML.电子邮件等各个文档管理领域,为全球.NET ...