【发布时间】:2016-04-03 04:19:30
【问题描述】:
在 Api 的 Hashmap 中发送字符串参数的集合。现在需要添加一个必须是图像的参数文件。
POST api 的主体如下所示:
Key1, Value1, Text
Key2, Value2, Text
Key3, Value3, File
我见过很多多部分请求的例子,但没有一个能解决问题。 寻找方法/示例。
【问题讨论】:
-
服务器期望什么?你也控制服务器吗?
在 Api 的 Hashmap 中发送字符串参数的集合。现在需要添加一个必须是图像的参数文件。
POST api 的主体如下所示:
Key1, Value1, Text
Key2, Value2, Text
Key3, Value3, File
我见过很多多部分请求的例子,但没有一个能解决问题。 寻找方法/示例。
【问题讨论】:
注意:这是将图像作为文件发送的另一种方法。
您可以尝试将 Image 转换为 BASE64 字符串,并将其作为字符串发送。
首先,将您的位图转换为字节数组:
//can use lower value than 100 for more compression or change compression format as JPEG
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bAOS);
byte[] byteArray = bAOS.toByteArray();
然后,将其编码为 BASE64 字符串:
String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);
最后把它作为一个字符串放到你的 hashmap 中。
【讨论】: