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 ...
随机推荐
- RubyMine安装、破解
经常安装东西,这是我安装过最快的ide破解版. 下载地址: http://www.jetbrains.com/ruby/download/index.html 破解序列号: name: rubymin ...
- org.eclipse.swt.SWTError: No more handles的解决办法
今天装了JBoss Tools 3.1 插件后,eclipse 打开jsp文件老是报错,或者要我关闭: org.eclipse.swt.SWTError: No more handles 网上找了两个 ...
- 前端模块化开发的规范:AMD与CDM
AMD, 异步模块定义. CMD,通用模块规范.
- Javascript中两种最通用的定义类的方法
在Javascript中,一切都是对象,包括函数.在Javascript中并没有真正的类,不能像C#,PHP等语言中用 class xxx来定义.但Javascript中提供了一种折中的方案:把对象定 ...
- poj 2531 Network Saboteur 解题报告
题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最 ...
- ssl原理及应用
今天学习网络通信,看到使用ssl(Secure Sockets Layer)进行加密,由于对ssl只是有些概念上的了解,对于具体应用原理.过程和如何使用不慎了解,于是学习了一番,总结如下: 1. 为什 ...
- 安装Sublime Text 3插件的方法(转自Rising的博文)
安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧,谢谢! 直接安装 安装Sublime text 2插件很方便,可 ...
- 单片机和Linux都想学_换个两全的方法学习单片机
本节教你如何学习单片机,如何选择合适的开发板和开发工具. 现在我们知道单片机是要学习的,那么怎么去学习单片机?在上一课我们说不要使用老一套的方法学习,实际上是指的两个问题. 第一:选择什么开发板: 第 ...
- css 样式引入的方法 link 与import的区别
<link> 元素所参考的样式用户可以自由的选择加以改变,而导入的样式表单就自动的与剩下的样式表融合在一起了 CSS与HTML文档结合的4中方法:1 使用<link>元素链接到 ...
- CreateThread创建线程 互斥量锁
HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,//SD:线程安全相关的属性,常置为NULL SIZE_T dwStackS ...