Okhttp: How to add multi-part form part using a loop?
I am using OKHttp to send multipart data. Here a piece is my code:
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM)
.addFormDataPart("arrival_to", object.optString("arrival_to"))
.addFormDataPart("centre_id", object.optString("centre_id"))
.addFormDataPart("departure_from", object.optString("departure_from"))
.addFormDataPart("expense_item_id", object.optString("expense_item_id"))
.addFormDataPart("expense_sub_item_id", object.getString("expense_sub_item_id"))
.addFormDataPart("file_list", object.optString("file_list"))
.addFormDataPart("journey_date", object.optString("journey_date"))
.addFormDataPart("passenger_age", object.optString("passenger_age"))
.addFormDataPart("passenger_gender", object.optString("passenger_gender"))
.addFormDataPart("passenger_name", object.optString("passenger_name"))
.addFormDataPart("personal_id", object.optString("personal_id"))
.addFormDataPart("remarks", object.optString("remarks"))
.addFormDataPart("request_amount", object.optString("request_amount"))
.addFormDataPart("request_status", object.optString("request_status"))
.addFormDataPart("travel_class", object.optString("travel_class"))
.addFormDataPart("vehicle_name_no", object.optString("vehicle_name_no"))
.addFormDataPart("vendor_id", object.optString("vendor_id"))
.addFormDataPart("vendor_type_id", object.optString("vendor_type_id"))
.addFormDataPart("within_budget", object.optString("within_budget"))
.addFormDataPart("file_list", attach_file, RequestBody.create(MediaType.parse(type), file))
.build();
The problem here is that I am adding the form data parts one by one which is ok here but I wanted to use a loop to add the form data part in case the dataset increased. Also I am making a seperate class containing the code to upload the data, how do I pass the key value pairs to that class should I use a Map?
android okhttp3
add a comment |
I am using OKHttp to send multipart data. Here a piece is my code:
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM)
.addFormDataPart("arrival_to", object.optString("arrival_to"))
.addFormDataPart("centre_id", object.optString("centre_id"))
.addFormDataPart("departure_from", object.optString("departure_from"))
.addFormDataPart("expense_item_id", object.optString("expense_item_id"))
.addFormDataPart("expense_sub_item_id", object.getString("expense_sub_item_id"))
.addFormDataPart("file_list", object.optString("file_list"))
.addFormDataPart("journey_date", object.optString("journey_date"))
.addFormDataPart("passenger_age", object.optString("passenger_age"))
.addFormDataPart("passenger_gender", object.optString("passenger_gender"))
.addFormDataPart("passenger_name", object.optString("passenger_name"))
.addFormDataPart("personal_id", object.optString("personal_id"))
.addFormDataPart("remarks", object.optString("remarks"))
.addFormDataPart("request_amount", object.optString("request_amount"))
.addFormDataPart("request_status", object.optString("request_status"))
.addFormDataPart("travel_class", object.optString("travel_class"))
.addFormDataPart("vehicle_name_no", object.optString("vehicle_name_no"))
.addFormDataPart("vendor_id", object.optString("vendor_id"))
.addFormDataPart("vendor_type_id", object.optString("vendor_type_id"))
.addFormDataPart("within_budget", object.optString("within_budget"))
.addFormDataPart("file_list", attach_file, RequestBody.create(MediaType.parse(type), file))
.build();
The problem here is that I am adding the form data parts one by one which is ok here but I wanted to use a loop to add the form data part in case the dataset increased. Also I am making a seperate class containing the code to upload the data, how do I pass the key value pairs to that class should I use a Map?
android okhttp3
add a comment |
I am using OKHttp to send multipart data. Here a piece is my code:
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM)
.addFormDataPart("arrival_to", object.optString("arrival_to"))
.addFormDataPart("centre_id", object.optString("centre_id"))
.addFormDataPart("departure_from", object.optString("departure_from"))
.addFormDataPart("expense_item_id", object.optString("expense_item_id"))
.addFormDataPart("expense_sub_item_id", object.getString("expense_sub_item_id"))
.addFormDataPart("file_list", object.optString("file_list"))
.addFormDataPart("journey_date", object.optString("journey_date"))
.addFormDataPart("passenger_age", object.optString("passenger_age"))
.addFormDataPart("passenger_gender", object.optString("passenger_gender"))
.addFormDataPart("passenger_name", object.optString("passenger_name"))
.addFormDataPart("personal_id", object.optString("personal_id"))
.addFormDataPart("remarks", object.optString("remarks"))
.addFormDataPart("request_amount", object.optString("request_amount"))
.addFormDataPart("request_status", object.optString("request_status"))
.addFormDataPart("travel_class", object.optString("travel_class"))
.addFormDataPart("vehicle_name_no", object.optString("vehicle_name_no"))
.addFormDataPart("vendor_id", object.optString("vendor_id"))
.addFormDataPart("vendor_type_id", object.optString("vendor_type_id"))
.addFormDataPart("within_budget", object.optString("within_budget"))
.addFormDataPart("file_list", attach_file, RequestBody.create(MediaType.parse(type), file))
.build();
The problem here is that I am adding the form data parts one by one which is ok here but I wanted to use a loop to add the form data part in case the dataset increased. Also I am making a seperate class containing the code to upload the data, how do I pass the key value pairs to that class should I use a Map?
android okhttp3
I am using OKHttp to send multipart data. Here a piece is my code:
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM)
.addFormDataPart("arrival_to", object.optString("arrival_to"))
.addFormDataPart("centre_id", object.optString("centre_id"))
.addFormDataPart("departure_from", object.optString("departure_from"))
.addFormDataPart("expense_item_id", object.optString("expense_item_id"))
.addFormDataPart("expense_sub_item_id", object.getString("expense_sub_item_id"))
.addFormDataPart("file_list", object.optString("file_list"))
.addFormDataPart("journey_date", object.optString("journey_date"))
.addFormDataPart("passenger_age", object.optString("passenger_age"))
.addFormDataPart("passenger_gender", object.optString("passenger_gender"))
.addFormDataPart("passenger_name", object.optString("passenger_name"))
.addFormDataPart("personal_id", object.optString("personal_id"))
.addFormDataPart("remarks", object.optString("remarks"))
.addFormDataPart("request_amount", object.optString("request_amount"))
.addFormDataPart("request_status", object.optString("request_status"))
.addFormDataPart("travel_class", object.optString("travel_class"))
.addFormDataPart("vehicle_name_no", object.optString("vehicle_name_no"))
.addFormDataPart("vendor_id", object.optString("vendor_id"))
.addFormDataPart("vendor_type_id", object.optString("vendor_type_id"))
.addFormDataPart("within_budget", object.optString("within_budget"))
.addFormDataPart("file_list", attach_file, RequestBody.create(MediaType.parse(type), file))
.build();
The problem here is that I am adding the form data parts one by one which is ok here but I wanted to use a loop to add the form data part in case the dataset increased. Also I am making a seperate class containing the code to upload the data, how do I pass the key value pairs to that class should I use a Map?
android okhttp3
android okhttp3
asked Nov 15 '18 at 5:28
user10655681
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Maybe something like this works?
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);
String keys = new String
"arrival_to",
"centre_id",
"departure_from",
"expense_item_id",
"...."
;
Arrays.stream(keys)
.forEach(key ->
builder.addFormDataPart(key, object.optString(key));
);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312978%2fokhttp-how-to-add-multi-part-form-part-using-a-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe something like this works?
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);
String keys = new String
"arrival_to",
"centre_id",
"departure_from",
"expense_item_id",
"...."
;
Arrays.stream(keys)
.forEach(key ->
builder.addFormDataPart(key, object.optString(key));
);
}
add a comment |
Maybe something like this works?
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);
String keys = new String
"arrival_to",
"centre_id",
"departure_from",
"expense_item_id",
"...."
;
Arrays.stream(keys)
.forEach(key ->
builder.addFormDataPart(key, object.optString(key));
);
}
add a comment |
Maybe something like this works?
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);
String keys = new String
"arrival_to",
"centre_id",
"departure_from",
"expense_item_id",
"...."
;
Arrays.stream(keys)
.forEach(key ->
builder.addFormDataPart(key, object.optString(key));
);
}
Maybe something like this works?
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);
String keys = new String
"arrival_to",
"centre_id",
"departure_from",
"expense_item_id",
"...."
;
Arrays.stream(keys)
.forEach(key ->
builder.addFormDataPart(key, object.optString(key));
);
}
answered Nov 15 '18 at 5:39
Cir0XCir0X
33229
33229
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312978%2fokhttp-how-to-add-multi-part-form-part-using-a-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown