2015-08-24 82 views
1

我有一個包含多行數據的arraylist,我希望從android傳遞到顯示它的PHP服務器。我將arraylist內容放入JSON對象中,在解析之前我將它傳遞給名稱 - 值對列表。只有保存在JSON對象中的arraylist的最後一行

我的問題是當我輸出收到的JSON的值。它只顯示最後的記錄。

PHP代碼:

<?php 


if($_POST) 
{ 
echo "Smething was sent"; 

$JSON_Entry = $_POST["Entry"]; 

$obj = json_decode($JSON_Entry); 

$i = 0; 

print_r($obj); 
} 

?> 

Java代碼:

 ArrayList<SalesReciepts> entryList = db.getSalesRecords(); 

     List<NameValuePair> postVars = new ArrayList<NameValuePair>(); 



     for (int i = 0; i < entryList.size(); i++) { 

      try { 
       JSONentry.put("id", String.valueOf(entryList.get(i).getEntryId())); 
       JSONentry.put("invoice",String.valueOf(entryList.get(i).getInvoice_id())); 
       JSONentry.put("product", String.valueOf(entryList.get(i).getProduct())); 
       JSONentry.put("qty", String.valueOf(entryList.get(i).getQty())); 
       JSONentry.put("total", String.valueOf(entryList.get(i).getTotal())); 
      } 
      catch(JSONException e) { 
       e.printStackTrace(); 
      } 

     } 



     JSONObject sent = new JSONObject(); 


     try { 
      sent.put("records", String.valueOf(JSONentry)); 
     } 
     catch(JSONException e) { 
      e.printStackTrace(); 
     } 


     postVars.add(new BasicNameValuePair("Entry", String.valueOf(sent))); 


     //Declare and Initialize Http Clients and Http Posts 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(POST_PRODUCTS); 

     //Format it to be sent 
     try { 
      httppost.setEntity(new UrlEncodedFormEntity(postVars)); 

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

     /* Send request and Get the Response Back */ 
     try { 

      HttpResponse response = httpclient.execute(httppost); 
      String responseBody = EntityUtils.toString(response.getEntity()); 


      Log.e("response:", responseBody); 

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

      Log.v("MAD", "Error sending... "); 


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

      Log.v("MAD", "Error sending... "); 


     } 

OUTPUT:

Smething was sent{"records":"{\"total\":\"1398.0\",\"product\":\"Carlsberg\",\"id\":\"0\",\"qty\":\"2\",\"invoice\":\"2.4082015083321E13\"}"} 

輸出顯示在過去的3行/記錄

回答

1

您需要爲每個循環迭代創建一個新的JSONentry,然後將其添加到您的JSONArray

更改代碼這樣的:

  ArrayList<SalesReciepts> entryList = db.getSalesRecords(); 

      List<NameValuePair> postVars = new ArrayList<NameValuePair>(); 

      JSONArray recordsJsonArray = = new JSONArray(); 


      for (int i = 0; i < entryList.size(); i++) { 

       try { 
        JSONObject JSONentry = new JSONObject(); // here you create a new JSONObject 

        JSONentry.put("id", String.valueOf(entryList.get(i).getEntryId())); 
        JSONentry.put("invoice",String.valueOf(entryList.get(i).getInvoice_id())); 
        JSONentry.put("product", String.valueOf(entryList.get(i).getProduct())); 
        JSONentry.put("qty", String.valueOf(entryList.get(i).getQty())); 
        JSONentry.put("total", String.valueOf(entryList.get(i).getTotal())); 

        recordsJsonArray.put(JSONentry); // here you add the item to your array 
       } 
       catch(JSONException e) { 
        e.printStackTrace(); 
       } 

      } 

      JSONObject sent = new JSONObject(); 

      try { 
       sent.put("records", String.valueOf(recordsJsonArray)); 
      } 
      catch(JSONException e) { 
       e.printStackTrace(); 
      } 

      postVars.add(new BasicNameValuePair("Entry", String.valueOf(sent))); 
1

你必須在ev後創建一個新的JSON入口紅圈。現在你只是一遍又一遍地重寫最後的設定值。

0

而不被一個Java專家,但我要說你需要 改變這一行以及隨後的 JSONentry.put( 「ID」,將String.valueOf(entryList.get(I).getEntryId())) ; 與「id []」 之類的東西,但再次 - 我不是一個JAVA專家,但它強烈地看起來像重寫相同的值反覆,因此只有最後一個被抓到PHP腳本。

0

您的JSONEntry是一個JSONObject。您需要創建一個JSONArray,您將放置不同的JSONEntry

相關問題