Android Retrofit 2.0框架上传图片解决方案
作者:lijiao 时间:2022-02-23 06:25:27
本文为大家分享了 Android Retrofit 2.0框架上传图片解决方案,具体内容如下
1.单张图片的上传
/**
* 上传一张图片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);
2.多张图片的上传
/**
* 上传三张图片
* @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中的可变参数解决之后,就只能传一张。不能多张。
@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接口
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对象
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调用上传的方法
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接收:
@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));
}
}
关于Android上传功能的更多内容请点击专题:Android上传操作汇总进行学习。
标签:Android,Retrofit,2.0框架,上传图片
0
投稿
猜你喜欢
C# 无边框窗体之窗体移动实现代码
2021-07-22 04:36:48
Java编程倒计时实现方法示例
2021-05-31 06:30:11
Java集合TreeSet用法详解
2023-11-10 22:53:34
C#统计字符串的方法
2021-06-12 17:00:12
Java中常用的设计模式之工厂模式详解
2021-07-04 15:33:37
java 中sendredirect()和forward()方法的区别
2021-11-07 18:39:28
通过Docker启动Solace并在Spring Boot通过JMS整合Solace的操作方法
2023-07-11 09:52:28
Springboot 通过FastJson实现bean对象和Json字符串互转问题
2021-11-14 05:10:06
Java中的装箱和拆箱深入理解
2023-02-22 08:18:10
Android将项目导出为Library并在项目中使用教程
2022-01-31 14:57:17
值得收藏的2017年Java开发岗位面试题
2023-11-29 15:22:01
JAVA OutputStreamWriter流的实现
2021-08-12 19:45:47
Java毕业设计实战之医院心理咨询问诊系统的实现
2022-07-04 19:02:21
C#异常执行重试的实现方法
2023-12-04 16:24:59
C#直线的最小二乘法线性回归运算实例
2022-05-03 11:19:33
Java用递归方法解决汉诺塔问题详解
2022-11-23 03:11:40
浅析Spring工厂的反射和配置文件
2023-06-22 20:52:23
分析JVM源码之Thread.interrupt系统级别线程打断
2023-07-31 17:15:23
c#创建windows服务(Windows Services)详细步骤
2023-06-11 19:05:22
Java String对象使用方法详解
2023-12-14 14:43:52