1

I am trying to send MultiPartEntity to the server to upload an image.

I have to send data to the server with a MultiPartEntity, The following is a sample request.

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

Does anyone have an idea on how to do this?

Please help me solve this.

Nicholas
  • 2,147
  • 3
  • 23
  • 31
Taruni
  • 1,671
  • 4
  • 22
  • 43
  • 2
    http://stackoverflow.com/questions/12422541/how-to-send-multiple-images-to-server-using-multipartentity-from-android – Hardik Joshi Oct 26 '12 at 06:28
  • Why don't you send it in the format: MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpEntity.addPart("id", new StringBody("12345")); mpEntity.addPart("image", new FileBody(imageFile, "photo test")); – Yogesh Somani Oct 26 '12 at 06:44
  • i tried that but i need to send "id" inside the "user" tag and "image" should be inside the "poster-photo" tag. @YogeshSomani – Taruni Oct 26 '12 at 07:01
  • @Taruni did you checked my answer? – Niranj Patel Oct 30 '12 at 04:39
  • @Taruni for uploading image in multi-part check this link http://stackoverflow.com/questions/4623507/post-valuse-and-upload-image-to-php-server-in-android/4633474#4633474 – Jaiprakash Soni Oct 31 '12 at 11:33

1 Answers1

3

There are two way to achieve your goal.

1)Using JSON

Android Side:- First thing is JSON format can support only string data, so you can add only string data. If you want to add image in JSON format so you have to convert Image to String. Image into bytearray

Server Side:- Decode byte string and convert in Image.

2)Using NameValuePair

Android Side:- Used NameValuePair instead of JSON, for this way follow link which HardikJoshi given in comment (Link)

Server Side:- Change server code JSON to NameValuePair.

I suggest Second Way is easy to implement & understand..

Community
  • 1
  • 1
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133