Aswin .A.S
Aswin .A.S

Reputation: 217

How to Post complex datas as MultiPart using Retrofit in Android?

I have data which is Array of Json Objects, I need to pass this as MultiPart Form Data. The data which i need to post is below :

{
    "name": "KIMS EKM",
    "latitude": "8.5605418",
    "longitude": "76.8810471",
    "state": "Kerala",
    "district": "Thiruvananthapuram",
    "city": "Leela Infopark",
    "landmark": "Kazhakuttom",
    "phone": "7293318484",
    "email": "[email protected]",
    "admin_name": "Aswin",
    "admin_phone": "7293318484",
    "admin_email": "[email protected]",
    "departments": [{
        "id": 1,
        "is_sunday": 1
    }, {
        "id": 2,
        "is_sunday": 1
    }, {
        "id": 3,
        "is_sunday": 1
    }, {
        "id": 4,
        "is_sunday": 1
    }, {
        "id": 5,
        "is_sunday": 1
    }, {
        "id": 6,
        "is_sunday": 1
    }, {
        "id": 7,
        "is_sunday": 1
    }, {
        "id": 8,
        "is_sunday": 1
    }, {
        "id": 9,
        "is_sunday": 0
    }, {
        "id": 10,
        "is_sunday": 0
    }, {
        "id": 11,
        "is_sunday": 0
    }],
    "all_day_service": [3, 4, 5],
    "emergency_services": [5],
    "special_services": ["BPD", "HCDX"],
    "accreditations": [2, 3]
}

Here the exact keys are used as params for multipart form Data. There are image needed to be posted, But the real problem is described below.

I have posted MultiPart form datas, but posting array of json objects is what i find tough. How can the "departments" & "all_day_service" field can be posted ?

Upvotes: 2

Views: 1100

Answers (2)

Hally Trần
Hally Trần

Reputation: 49

    -----------------------------------com.example.Department.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Department {

@SerializedName("id")
@Expose
public Integer id;
@SerializedName("is_sunday")
@Expose
public Integer isSunday;

}
-----------------------------------com.example.ResponseContracts.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ResponseContracts {

@SerializedName("name")
@Expose
public String name;
@SerializedName("latitude")
@Expose
public String latitude;
@SerializedName("longitude")
@Expose
public String longitude;
@SerializedName("state")
@Expose
public String state;
@SerializedName("district")
@Expose
public String district;
@SerializedName("city")
@Expose
public String city;
@SerializedName("landmark")
@Expose
public String landmark;
@SerializedName("phone")
@Expose
public String phone;
@SerializedName("email")
@Expose
public String email;
@SerializedName("admin_name")
@Expose
public String adminName;
@SerializedName("admin_phone")
@Expose
public String adminPhone;
@SerializedName("admin_email")
@Expose
public String adminEmail;
@SerializedName("departments")
@Expose
public List<Department> departments = null;
@SerializedName("all_day_service")
@Expose
public List<Integer> allDayService = null;
@SerializedName("emergency_services")
@Expose
public List<Integer> emergencyServices = null;
@SerializedName("special_services")
@Expose
public List<String> specialServices = null;
@SerializedName("accreditations")
@Expose
public List<Integer> accreditations = null;

}

http://www.jsonschema2pojo.org/ From here, you make a model, send request by @body

@POST("/api/v1/post url here")
Call<Response> registerUser(@Body ResponseContracts body);

Upvotes: 0

Antonis Radz
Antonis Radz

Reputation: 3097

You have to make models of data parts and it should look something like this:


 @Multipart
    @POST("request")
    Call<ResponseBody> upload(
        @Part("item1") RequestBody item1,
        @Part("item2") RequestBody item2,
        @Part("item3") RequestBody item3
    );

Also there is @PartMap Map<String, RequestBody> map

Upvotes: 1

Related Questions