【问题标题】:only last row of arraylist being saved in JSON object只有最后一行 arraylist 被保存在 JSON 对象中
【发布时间】:2015-11-17 22:12:10
【问题描述】:

我有一个包含多行数据的数组列表,我希望将其从 android 传递到显示它的 PHP 服务器。我将 arraylist 内容放在一个 JSON 对象中,然后在解析之前将其传递给一个名称-值-对列表。

我的问题是当我输出收到的 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... ");


        }

输出:

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

输出显示 3 行/记录中的最后一个

【问题讨论】:

    标签: java php android json arraylist


    【解决方案1】:

    您必须在每个循环之后创建一个新的 JSONentry。现在你只是一遍又一遍地覆盖最后一个设置值。

    【讨论】:

      【解决方案2】:

      不是 Java 专家,但我会说你需要 更改此行和以下行 JSONentry.put("id", String.valueOf(entryList.get(i).getEntryId())); 像“id []”这样的东西 但再说一遍 - 我不是 JAVA 专家,但看起来你一遍又一遍地覆盖相同的值,因此只有最后一个值被 PHP 脚本捕获。

      【讨论】:

        【解决方案3】:

        您的 JSONEntry 是一个 JSONObject。您需要创建一个 JSONArray,您将在其中放置不同的 JSONEntry

        【讨论】:

          【解决方案4】:

          您需要为每个循环迭代创建一个新的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)));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-19
            • 2014-09-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多