2015-08-24 135 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

相关问题