当前位置:首页 » 文件管理 » retrofit图文上传

retrofit图文上传

发布时间: 2022-09-26 03:25:21

‘壹’ android retrofit 2.0 怎么同时上传多张

/**
* 上传一张图片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);

‘贰’ retrofit可以上传大文件吗

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接收:

@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));
}

}

‘叁’ 初学者问,怎样通过url传递动态变化的参数

其实我们用 POST 的场景相对较多,绝大多数的服务端接口都需要做加密、鉴权和校验,GET 显然不能很好的满足这个需求。使用 POST 提交表单的场景就更是刚需了,怎么做呢?

@FormUrlEncoded
@POST("/")
Call<ResponseBody> submit(
@Field("name") String name,
@Field("occupation") String occupation);12345

其实也很简单,我们只需要定义上面的接口就可以了,我们用 Field 声明了表单的项,这样提交表单就跟普通的函数调用一样简单直接了。

与Query一样,如果你有很多Field参数,不要怕,赶紧试试FieldMap吧。

Part & PartMap(POST请求)
这个是用来上传文件的。有了 Retrofit,妈妈再也不用担心文件上传费劲了~~~

public interface FileUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description, @Part MultipartBody.Part file);
}1234567

如果你需要上传文件,和我们前面的做法类似,定义一个接口方法,需要注意的是,这个方法不再有 @FormUrlEncoded 这个注解,而换成了 @Multipart,后面只需要在参数中增加 Part 就可以了。也许你会问,这里的 Part 和 Field 究竟有什么区别,其实从功能上讲,无非就是客户端向服务端发起请求携带参数的方式不同,并且前者可以携带的参数类型更加丰富,包括数据流。

也正是因为这一点,我们可以通过这种方式来上传文件,下面我们就给出这个接口的使用方法:

‘肆’ android retrofit上传二进制流 byte[] img

以下是图片上传方式:
接口写法:
Java code?
1
2
3

@Multipart
@POST("/user/addLicenseInfo")
void addLicenseInfo(@QueryMap Map<String, Object> options, @Part("file") TypedFile file, Callback<JsonElement> response);

实现写法:
Java code?
1
2
3
4
5

API api = mRegisterActivity.createAPI();
Map<String, Object> options = new HashMap<String, Object>();
options.put("mobile",photoNumber);
TypedFile typedImage = new TypedFile(getMIMEType(pictureFile), pictureFile);
api.addLicenseInfo(options,typedImage,new Callback<JsonEleme

‘伍’ 为什么Retrofit以Mutipart上传参数时,String参数会多一对双引号

“@Part(“data”) String des”在Post请求中默认的Content-Type类型是“application/json”,这就说明我们在接口中不能再使用@Part注解了

@Multipart
@POST("userPhoto")
Observable<BaseHttpResult<String>> uploadMultipleTypeFile(@PartMap Map<String, RequestBody> params);

Map<String, RequestBody> bodyMap = new HashMap<>();
bodyMap.put("photo"; filename=""+file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
bodyMap.put("userId", toRequestBody(userId));
bodyMap.put("serialNumber", toRequestBody(serialNumber));

public static RequestBody toRequestBody(String value) {

‘陆’ retrofit 怎么设置长连接 文件类型

想实现这一目标其实很简单,随便打开一个文件夹,将查看方式改为大图标,然后打开文件夹选项,打开查看选项卡,点击应用到文件夹,就可以了,具体操作步骤如下: 1、随便打开一个文件夹,点击红色方框中的向下三角,更多选项, 将查看类型改为大图标; 2、“工具”下拉菜单,选择文件夹选项; 3、选择查看选项卡,点击应用到文件夹, 4、在弹出的提示框中选择“是(Y)”,然后打开另外一个文件夹,看看自己的目的是否已经达到。

‘柒’ 怎么解决retrofit上传文件导致内存不足

保存文件,重要的备份。然后查看资源管理器,干掉内存大的,或者重启。

‘捌’ android retrofit 上传进度requestbody writeto 为什么会调用两次

用Retrofit发送网络请求和解析json的实例Retrofit是Android的一个非常好用的开源HTTPRequest。现在介绍一下Retrofit是如何使用的。。。。首先是导入Retrofit包,dependencies{compilefileTree(dir:'libs',include:['*.jar'])compile

热点内容
脚本怎么归档 发布:2024-10-09 16:08:07 浏览:295
云平台搭建服务器 发布:2024-10-09 16:03:47 浏览:635
用阿里云搭建正向代理服务器 发布:2024-10-09 15:53:07 浏览:505
手机qq空间缓存清理缓存 发布:2024-10-09 15:51:49 浏览:351
pc泰拉瑞亚服务器ip 发布:2024-10-09 15:45:18 浏览:797
安卓怎么延时 发布:2024-10-09 15:37:51 浏览:453
android音源 发布:2024-10-09 14:55:19 浏览:119
预编译sql怎么模糊查询 发布:2024-10-09 14:31:24 浏览:217
我的世界服务器被占违法吗 发布:2024-10-09 14:27:50 浏览:390
资源配置如何发生作用 发布:2024-10-09 14:27:48 浏览:685