如何使用下面类型的Json的Volly Library在Android中发布JSON对象?

时间:2016-10-04 07:25:45

标签: android

这是我的代码:

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

        }
    ]
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

更新:在您的gradle(应用级别)中添加此内容

    Class<?> cls=WifiManager.class; 
   for (Method method:cls.getDeclaredMethods()){ 
   String methodName=method.getName(); 
   if (methodName.equals("getWifiApState")){ getWifiApState=method; }
   else if (methodName.equals("isWifiApEnabled")){ isWifiApEnabled=method; }
   else if (methodName.equals("setWifiApEnabled")){ setWifiApEnabled=method; }
   else if (methodName.equals("getWifiApConfiguration")) {       getWifiApConfiguration=method; } }

Bansal 开发。

您的json数据

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

然后你的jsonRequest

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

答案 2 :(得分:0)

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