2016-10-04 67 views
0

這裏是我的代碼:如何發佈JSON對象在Android下使用Volly Library下面的Json類型?

{ 
    "VisitorDetails":[ 
     { 
      "Name":"Ramesh", 
      "Gender":"Male", 
      "Age":24, 
      "MobileNo":9502230173, 
      "LandLine":"040140088", 
      "EmailId":"[email protected]", 
      "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" 

     } 
    ] 
} 
+0

請人知道答案告訴我>我freshere在android系統 –

+0

創建JSOnArray把[]括號之間的所有數據,然後創建一個json對象,並將這個對象關鍵字中的arrray作爲VisitorDetails。 –

回答

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

更新:在您的gradle這個添加此(應用級)

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

邦薩爾 ANS發展。

您的JSON數據

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

然後你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; 
      } 
+0

我使用這個 –

+1

kk我將更新mcxiaoke.volley.library以編譯'com.android.volley:volley:1.0.0'謝謝.......... –

+0

thankx @ M.praveen我只是複製我的代碼。現在我編輯 – siva35

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

但是這是錯誤Bad Authentication is here, –

+0

我正在使用com.mcxiaoke.volley:library-aar:1.0.0 library –

+0

com.mcxiaoke.volly:library-arr:1.0.0庫已棄用。 – siva35