问题
I'm using retrofit as my network library, and I want to send a JSON array as x-www-form-urlencoded
, but I don't know how to do that.
Here is my request API :
@POST("book")
@FormUrlEncoded
Call<BookTicket> BookFlight(@Header("Authorization") String authorization,
@Header("Content-Type") String content_type,
@Field("rootType") Integer rootType ,
@Field("BookingStep2") ArrayList<JSONObject> BookingStep2
);
Here is a sample request :
{
"rootType": 1,
"BookingStep2": {
"OriginFlightSegment": {
"FlightNumber": "sample",
"Source": "sample",
"TotalTime": "sample",
"FareDatas": [
{
"PassengerType": 1,
"Endorsements": "sample",
"FareBasisCode": "sample"
},
{
"PassengerType": 1,
"Endorsements": "sample",
"FareBasisCode": "sample"
}
]
}
}
}
Is it right to send JSON array as field in retrofit?
回答1:
To post array object as form-url_encoded just use this in retrofit.
@POST(ApiConstants.JOB_CARD_SAVE)
@FormUrlEncoded
Call<MasterResponse<JobCardSaveResponse>> jobCardSave(@FieldMap Map<String, Object> _jobCardRequest,
@Field("qty[]") List<String> qty,
@Field("service_id[]") List<String> service_id);
Just put your array object in @field.
来源:https://stackoverflow.com/questions/50897508/android-retrofit-send-array-as-x-www-form-urlencoded