简体   繁体   中英

How to POST JSON Object In Android using Volly Library below type of Json?

Here is my code :

   {
     "VisitorDetails":[
         {
            "Name":"Ramesh",
            "Gender":"Male",
            "Age":24,
            "MobileNo":9502230173,
            "LandLine":"040140088",
            "EmailId":"rameshkandula24@gmail.com",
            "CreatedOn":"08-25-2016",
            "Address":"Hyderabad",
            "Profession":"Software",
            "FamilyMembers":5,
            "HomeTown":"Gannavaram",
            "MedicalHealing":"Noooooo",
            "Isinterestedwithcompanies":1,
            "IsBetterlivingStandards":1,
            "IsInterestedinConference":1,
            "VisitorExcites":[1,2,3]
            "jsonkey" : "rUinterested"

        }
    ]
}
try {

    JSONArray arry = new JSONArray();

    JSONObject iner = new JSONObject();

    iner.put("Name", "nmae");

    //all detail to iner

    arry.put(iner);

    JSONObject outer = new JSONObject();

    outer.put("VisitorDetails", arry);
}

catch (Exception e){
}

Update : add this in your gradle(app level)

compile 'com.android.volley:volley:1.0.0'

developing from Bansal ans .

your json data

JSONArray arry = new JSONArray();
        try {
            JSONObject jsonobject_one = new JSONObject();

            jsonobject_one.put("Name", "Name");
// add all details like this

arry.put(jsonobject_one);
            JSONObject jsonobject_TWO = new JSONObject();
            jsonobject_TWO.put("VisitorDetails", arry);

        }catch (JSONException e) {
                e.printStackTrace();
        }

Then Your jsonRequest

JsonArrayRequest jsonArryReq = new JsonArrayRequest(
        Request.Method.POST,url, arry,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());


            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());

            }
        }) {

    /**
     * Passing some request headers
     * */
   @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> param = new HashMap<String, String>();
                if (!GetMethod) {
                    param.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                    return param;
                }
                return param;
            }
 final JSONObject jsonObject=new JSONObject();
            try {
                JSONArray jsonArray=new JSONArray();

                JSONObject innerobject=new JSONObject();

                innerobject.put("Name",Name);
                innerobject.put("Address",Country);

                jsonArray.put(innerobject);

                jsonObject.put("VisitorDetails",jsonArray);


            }catch (Exception e){
                e.printStackTrace();
            }

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // do something...
                    Toast.makeText(MainActivity.this, "your data successfully register", Toast.LENGTH_SHORT).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // do something...
                    Toast.makeText(MainActivity.this, "your data not register", Toast.LENGTH_SHORT).show();
                }
            }) {

                /**
                 * Passing some request headers
                 */
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> param = new HashMap<String, String>();

                        param.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                        return param;
                }
            };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM