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 ...
随机推荐
- 如何去除Office Excel的密码保护?
企图更改Excel文件内容,然而却弹出如下提示: 根据提示,我尝试解除保护表,却要求输入密码: 这就尴尬了=_=密码不是我设定的 问了度娘,找到了解决方案 将Excel文件扩展名更改为rar, 使用压 ...
- GSON的简单示例
https://github.com/google/gson package com.example.wolf; import com.google.gson.JsonArray; import co ...
- C# delegate Action<T> lambda表达式
转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...
- js对闭包的理解
原文链接http://www.jb51.net/article/24101.htm,讲的很好,清晰明了.
- Python机器视觉编程常用数据结构与示例
本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slicing).加(adding).乘(multi ...
- if_test.py
strings=['xxaa','xuo','fwefxxx','woeuxxfei'] print(strings) #替换方法1 for string in strings: if 'xx' in ...
- Spss22安装与破解教程
Spss22安装与破解教程 Spss22安装与破解教程 1.下载安装包 可以去IBM官网.人大论坛等网站下载,全部文件应包括spss22安装包(含32位及64位)和破解文件,这里提供一个64位的百度网 ...
- codeforces round 422 div2 补题 CF 822 A-F
A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL ...
- 清理c盘的文件
C:/Users/用户名/AppData里面默认有三个文件夹,分别是Local,LocalLow,Roaming,简单地来说,都是用来存放软件的配置文件和临时文件的,里面有很多以软件公司或者软件名称命 ...
- html行内要素与块级要素
行内要素:在一行里,不可设置width和height,不能上下外铺(margin) span 块状要素,标准的 div