2014-06-17 68 views
1

我在數據庫中有一些數據,我想在一個JSONAndroid的JSON對象和JSON數組發送到httppost

 {"name":"Ram","age":20}, 
     {"name":"michael","age":25}, 
     {"name":"Suma","age":28} 

我想所有記錄發送到Web API同時發送數據。現在我正在一個接一個的發送如何發佈這個json.How將json對象動態地分配給一個json數組。

 JSONObject obj = new JSONObject(); 
    try { 

    obj.put("Name","Ram"); 
    obj.put("age",20); 

    HttpContext httpContext = new BasicHttpContext(); 
    HttpParams httpParameters = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParameters, 2000); 
    HttpConnectionParams.setSoTimeout(httpParameters, 2000); 
    HttpClient httpClient = new DefaultHttpClient(httpParameters); 
    HttpPost httpPost = new HttpPost("myurl"); 
    try { 
     StringEntity se = new StringEntity(obj.toString()); 
     httpPost.setEntity(se); 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 
     HttpResponse response = httpClient.execute(httpPost, httpContext); //execute your  HttpEntity entity = response.getEntity(); 
     String jsonString = EntityUtils.toString(entity); //if response in JSON format 
     System.out.println("Respone: "+jsonString); 
+0

創建多一個JSONObjects JSON數組,並通過httppost –

+0

你能回答它傳遞服務器對我的問題這將是greate如果你可以 – Comrade

+0

更具體你想要什麼.. –

回答

1

試試這個創建JSON數組..

JSONArray myArray = new JSONArray(); 

JSONObject j = new JSONObject(); 
    j.put("key",value); 
    j.put("key",value); 
    myarray.put(j); 
JSONObject j = new JSONObject(); 
    j.put("key",value); 
    j.put("key",value); 
    myarray.put(j); 
JSONObject j = new JSONObject(); 
    j.put("key",value); 
    j.put("key",value); 
    myarray.put(j); 

JSONObject finalObject = new JSONObject(); 
finalObject.put("Name", myarray); 

,然後通過finalObject它作爲字符串

+0

不適合我...我的最後調用編碼數組作爲字符串...我不知道爲什麼 –