0

anyone can suggest me how to POST volley JSONArray body like

{
 "mobileNo":"9876543210",
 "dobDocuments" : [
 "http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/dob_proofs/DOB Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/dob_proofs/DOB Proof2.jpg"
 ],
 "educationDocuments" : [
 "http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/edu_proofs/EDU Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/edu_proofs/EDU Proof2.jpg"
 ],
 "addressDocuments" : [
 "http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/add_proofs/ADD Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/add_proofs/ADD Proof2.jpg"
 ]
}

for this instance I've Hashmap which contains this array data.

I searched lot but not getting proper solution for this type. Thank you..!

asked May 2, 2018 at 6:00

2 Answers 2

1

Try this

 JSONObject sendObject = new JSONObject();
 try {
 JSONArray dobDocuments = new JSONArray();
 dobDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 dobDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 JSONArray educationDocuments = new JSONArray();
 educationDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 educationDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 JSONArray addressDocuments = new JSONArray();
 addressDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 addressDocuments.put("https://stackoverflow.com/questions/50128021/volley-post-method-jsonarray");
 sendObject.put("dobDocuments", dobDocuments);
 sendObject.put("educationDocuments", addressDocuments);
 sendObject.put("addressDocuments", addressDocuments);
 sendObject.put("mobileNo", "9876543210");
 } catch (JSONException e) {
 }
 Log.e("JSONObject",sendObject.toString());

OUTPUT

{
 "dobDocuments": ["https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray", "https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray"],
 "educationDocuments": ["https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray", "https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray"],
 "addressDocuments": ["https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray", "https:\/\/stackoverflow.com\/questions\/50128021\/volley-post-method-jsonarray"],
 "mobileNo": "9876543210"
}
answered May 2, 2018 at 6:10
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use model classes and comfortable with GSON. Add this to build.gradle

implementation 'com.google.code.gson:gson:2.8.4'

Create class for your request/response

class MyRequest {
 private String mobileNo;
 private String[] dobDocuments;
 private String[] educationDocuments;
 private String[] addressDocuments;
 public String getMobileNo() {
 return mobileNo;
 }
 public void setMobileNo(String mobileNo) {
 this.mobileNo = mobileNo;
 }
 public String[] getDobDocuments() {
 return dobDocuments;
 }
 public void setDobDocuments(String[] dobDocuments) {
 this.dobDocuments = dobDocuments;
 }
 public String[] getEducationDocuments() {
 return educationDocuments;
 }
 public void setEducationDocuments(String[] educationDocuments) {
 this.educationDocuments = educationDocuments;
 }
 public String[] getAddressDocuments() {
 return addressDocuments;
 }
 public void setAddressDocuments(String[] addressDocuments) {
 this.addressDocuments = addressDocuments;
 }
 }

Now create JSONObject

try {
 MyRequest myRequest = new MyRequest();
 myRequest.setMobileNo("9876543210");
 myRequest.setDobDocuments(new String[] {"http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/dob_proofs/DOB Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/dob_proofs/DOB Proof2.jpg"});
 myRequest.setEducationDocuments(new String[]{ "http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/edu_proofs/EDU Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/edu_proofs/EDU Proof2.jpg"});
 myRequest.setAddressDocuments(new String[]{"http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/add_proofs/ADD Proof1.jpg","http://server.com/test/uploads/users/5ae699bb8ec8d8218f18c3b4/5ae699f58ec8d8218f18c3b5/add_proofs/ADD Proof2.jpg"});
 JSONObject jsonObject = new JSONObject(new Gson().toJson(myRequest));
 Log.e("jsonObject", jsonObject.toString());
 } catch (JSONException e) {
 e.printStackTrace();
 }
answered May 2, 2018 at 6:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.