Android Retrofit 框架上传多张图片解决方案

方案:

解决的方法是:使用Retrofit的MulipartTypedOutput

接口声明如下,注意此时不要再使用@FormUrlEncoded 和 @Multipart 注解了

@POST(PATH + "submitOrderExt.do")
	public void submitOrderMul(@Body MultipartTypedOutput multipartTypedOutput,
			BaseCallback<BaseCallbackModel> baseCallback);


MulipartTypedOutput 参数的装配示例代码如下:

/// 新的实现demo
		MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
		multipartTypedOutput.addPart("stID", new TypedString(stID));
		multipartTypedOutput.addPart("stName", new TypedString(stName));
		multipartTypedOutput.addPart("uID", new TypedString(uID));
		multipartTypedOutput.addPart("uLoginMail", new TypedString(uLoginMail));
		multipartTypedOutput.addPart("areaID", new TypedString(areaID));
		multipartTypedOutput.addPart("areaName", new TypedString(areaName));
		multipartTypedOutput.addPart("oUserName", new TypedString(oUserName));
		multipartTypedOutput.addPart("oUserTel", new TypedString(oUserTel));
		multipartTypedOutput.addPart("oAddress", new TypedString(oAddress));
		multipartTypedOutput.addPart("oRepairContent", new TypedString(oRepairContent));
		multipartTypedOutput.addPart("oTimeStart", new TypedString(mTimeStart));
		multipartTypedOutput.addPart("oTimeEnd", new TypedString(mTimeEnd));
		for (Map.Entry<String, Uri> entry : oSceneImgUriMap.entrySet()) {
			multipartTypedOutput.addPart("oSceneImg", new TypedFile("image/jpeg", new File(GetPathFromUri4kitkat.getPath(OrderActivity.this, entry.getValue()))));
		}
		if(null != voiceFile){
			multipartTypedOutput.addPart("oVoice", voiceFile);
		}

 


说明:

有说Retrofit 2.0.0-beta1 之后 MultipartTypedOutput  不再使用转而使用@PartMap

Updates

MultipartTypedOutput no longer exists in Retrofit 2.0.0-beta1

For those who want to upload multiple images now can use with @PartMap, reference link javadoc

参考出处:

[1] http://stackoverflow.com/questions/31212513/android-retrofit-post-request-with-json-payload-and-images-attached

[2] http://stackoverflow.com/questions/25249042/retrofit-multiple-images-attached-in-one-multipart-request/25260556#25260556



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值