【问题标题】:MultiPartEntity along with plain text in androidMultiPartEntity 以及 android 中的纯文本
【发布时间】:2012-10-16 10:02:26
【问题描述】:

我正在尝试将MultiPartEntity 发送到服务器以上传图像。

我必须使用MultiPartEntity 向服务器发送数据,以下是示例请求。

{
  "user": {
    "id": "12345",
    "primary_account_id": "43566"
  },
  "poster_photo": {
    "title": "photo test",
    "image": *uploaded image 
  }
}

有人知道怎么做吗?

请帮我解决这个问题。

【问题讨论】:

  • 为什么不发送格式:MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpEntity.addPart("id", new StringBody("12345")); mpEntity.addPart("image", new FileBody(imageFile, "photo test"));
  • 我试过了,但我需要在“user”标签内发送“id”,而“image”应该在“poster-photo”标签内。 @YogeshSomani
  • @Taruni 你检查我的答案了吗?
  • @Taruni 用于分段上传图片,请查看此链接stackoverflow.com/questions/4623507/…

标签: java android http multipartentity


【解决方案1】:

有两种方法可以实现您的目标。

1)使用 JSON

Android端:- 首先是JSON格式只能支持字符串数据,所以只能添加字符串数据。如果要添加 JSON 格式的图像,则必须转换 Image to StringImage into bytearray

服务器端:-解码字节串并转换成Image。

2)使用 NameValuePair

Android 端:- 使用 NameValuePair 而不是 JSON,为此请点击 HardikJoshi 在评论中给出的链接 (Link)

服务器端:- 将服务器代码 JSON 更改为 NameValuePair。

我建议第二种方式易于实施和理解..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2011-05-15
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多