Метод залпового сообщения JSONArray - PullRequest
0 голосов
/ 02 мая 2018

любой может подсказать мне, как 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"
    ]
}

для этого экземпляра у меня есть Hashmap, который содержит данные этого массива.

Я искал много, но не нашел правильного решения для этого типа. Спасибо..!

Ответы [ 2 ]

0 голосов
/ 02 мая 2018

Если вы хотите использовать модели классов и комфортно с GSON. Добавьте это в build.gradle

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

Создать класс для вашего запроса / ответа

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;
        }
    }

Теперь создайте 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();
                }
0 голосов
/ 02 мая 2018

Попробуйте это

    JSONObject sendObject = new JSONObject();
    try {

        JSONArray dobDocuments = new JSONArray();
        dobDocuments.put("/10287797/metod-zalpovogo-soobscheniya-jsonarray");
        dobDocuments.put("/10287797/metod-zalpovogo-soobscheniya-jsonarray");

        JSONArray educationDocuments = new JSONArray();
        educationDocuments.put("/10287797/metod-zalpovogo-soobscheniya-jsonarray");
        educationDocuments.put("/10287797/metod-zalpovogo-soobscheniya-jsonarray");


        JSONArray addressDocuments = new JSONArray();
        addressDocuments.put("/10287797/metod-zalpovogo-soobscheniya-jsonarray");
        addressDocuments.put("/10287797/metod-zalpovogo-soobscheniya-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"
}
...