Volley post方法JSONArray

时间:2018-05-02 06:00:54

标签: java android json post android-volley

任何人都可以建议我如何 POST排球JSONArray 身体像

{
    "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 :(得分:1)

试试这个

    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());

<强>输出

{
    "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"
}

答案 1 :(得分:1)

如果您想使用模型类并且熟悉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();
                }