Android Retrofit 2.0框架上传文件解决方案(xjh测试可以)
来源:互联网 发布:php大众点评源码 编辑:程序博客网 时间:2024/06/11 10:20
本文为大家分享了 Android Retrofit 2.0框架上传图片解决方案,具体内容如下
1.单张图片的上传
1
2
3
4
5
6
7
8
9
10
/**
* 上传一张图片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST
(
"/upload"
)
Call<String> uploadImage(
@Part
(
"fileName"
) String description,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs);
2.多张图片的上传
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 上传三张图片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST
(
"/upload"
)
Call<String> uploadImage(
@Part
(
"fileName"
) String description,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs1,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs3);
注意:目前是提供传3张,要想多上传目前我发现的方法就是想要多传一张,就多增加一个参数
@Part("file\"; filename=\"image.png\"")RequestBody imgs,以此类推。
大家看到上面觉得写法很漏,但是用于能力有限,只能想到这样。用Java中的可变参数解决之后,就只能传一张。不能多张。
1
2
3
4
@Multipart
@POST
(
"/upload"
)
Call<String> uploadImage(
@Part
(
"fileName"
) String description,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody ...imgs);
调用:
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);
这样写看上去很是高端,不幸的是只能传一张
3.最后是实现胡过程
3.1创建FileUploadService接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public
interface
FileUploadService {
/**
* 上传一张图片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST
(
"/upload"
)
Call<String> uploadImage(
@Part
(
"fileName"
) String description,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs);
/**
* 上传三张图片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST
(
"/upload"
)
Call<String> uploadImage(
@Part
(
"fileName"
) String description,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs1,
@Part
(
"file\"; filename=\"image.png\""
)RequestBody imgs3);
}
3.2创建Retrofit对象
1
2
3
4
5
6
7
private
static
final
Retrofit sRetrofit =
new
Retrofit .Builder()
.baseUrl(ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
// .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器
.build();
private
static
final
FileUploadService apiManager = sRetrofit.create(FileUploadService.
class
);
3.3调用上传的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public
static
void
upload(String path){
String descriptionString =
"hello, this is description speaking"
;
String[] m =
new
String[
2
];
m[
0
]=
"share.png"
;
m[
1
]=
"Screenshot_20160128-140709.png"
;
File[] ssssss=
new
File[
2
];
File file1 =
new
File(
"/storage/emulated/0/sc/share.png"
);
File file =
new
File(
"/storage/emulated/0/Pictures/ScreenShots/Screenshot_20160128-140709.png"
);
ssssss[
0
]=file;
ssssss[
0
]=file1;
RequestBody requestBody[] =
new
RequestBody[
3
];
RequestBody requestBody1 =
RequestBody.create(MediaType.parse(
"multipart/form-data"
), file);
RequestBody requestBody2 =
RequestBody.create(MediaType.parse(
"multipart/form-data"
), file1);
requestBody[
0
]=requestBody1;
requestBody[
1
]=requestBody2;
Call<String> call = apiManager.uploadImage( m[
0
],requestBody1,requestBody2,
null
);
call.enqueue(
new
Callback<String>() {
@Override
public
void
onResponse(Response<String> response, Retrofit retrofit) {
Log.v(
"Upload"
, response.message());
Log.v(
"Upload"
,
"success"
);
}
@Override
public
void
onFailure(Throwable t) {
Log.e(
"Upload"
, t.toString());
}
});
}
4.服务器段代码:
服务器用的是struts接收:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Controller
public
class
GetToken
extends
ActionSupport {
/**
*
*/
private
static
final
long
serialVersionUID = 1L;
private
File[] file;
private
String[] fileName;
public
File[] getFile() {
return
file;
}
public
void
setFile(File[] file) {
this
.file = file;
}
public
String[] getFileName() {
return
fileName;
}
public
void
setFileName(String[] fileName) {
this
.fileName = fileName;
}
@Action
(
"/upload"
)
public
void
login() {
System.out.println(
"------"
+Arrays.toString(file));
System.out.println(
"------"
+Arrays.toString(fileName));
}
}
0 0
- Android Retrofit 2.0框架上传文件解决方案(xjh测试可以)
- Android Retrofit 2.0框架上传图片解决方案
- Android Retrofit 2.0框架上传图片,视频解决方案
- Android Retrofit 框架上传多张图片解决方案
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Retrofit 2.0文件上传
- Android Retrofit 上传文件图片
- Retrofit文件上传与下载(xjh 这应该是retrofit1的代码 亲测不行)___http://www.ithao123.cn/content-7813575.html
- Retrofit 2.0 文件上传类型
- Retrofit 2.0 单文件、多文件上传
- 一个基于Retrofit的单文件上传、下载框架
- Retrofit 2.0 文件上传类型错误
- 使用Retrofit上传文件
- Retrofit上传文件
- PHP相关1
- 列表属性
- ERROR: process_status_message: bad node [vm183-9] in message
- LeetCode-313.Super Ugly Number
- windows平台上的NDK开发
- Android Retrofit 2.0框架上传文件解决方案(xjh测试可以)
- JAVA WEB 浏览器兼容问题汇总
- istringstream的用法
- 小米、金山、猎豹、YY一起崩塌:“雷军帝国”何去何从?
- Android M 新的运行时权限开发者需要知道的一切
- jquery评论留言代码
- NMON
- VM 虚拟机迁移
- 网页正文及内容提取算法