使用Retrofit 中 @Multipart 上传图像
1.接口设置
/**
* 修改设备图标
* */
@Multipart
@POST("v2/family/device/icon"+validatte)
ObservablegetFamilyDeviceicon(@Header("Accept") String Accept,
@Header("tenantId")int tenantId,
@Query("familyId") Integer familyId,
@Query("deviceId") Integer deviceId,
@Part List file,
@Header("token") String token,
@Header("userId") String userId);
2.file按表单形式上传
/**
* 修改设备图标
* */
public static ObservablegetFamilyDeviceicon(Integer familyId, Integer deviceId, File file, String token, String userId) {
MultipartBody.Builder builder =new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part image = MultipartBody.Part.createFormData("image", file.getName(),requestBody);
// builder.addFormDataPart();
builder.addFormDataPart("file",file.getName(),requestBody);
List parts = builder.build().parts();
return API().getFamilyDeviceicon("application/vnd.nst.app-v1.0+json", ConstantUtils.TENANTID, familyId, deviceId, parts, token, userId)
.delay(DELAYTIME, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
网友评论