java - 仅 arraylist 的最后一行保存在 JSON 对象中

标签 java php android json arraylist

我有一个包含多行数据的数组列表,我希望将其从 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 行/记录

最佳答案

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

关于java - 仅 arraylist 的最后一行保存在 JSON 对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32183122/

相关文章:

java - 在 javaFX 中自动滚动

java - 如何将库(不可编辑的.java/.class文件)转换为可编辑的.java文件(项目中的标准类)?

php - 回应 mysqli_connect_errno 不工作

android - Android 中的嵌套形状

android - 无法访问本地 aar 库中的自定义 View ?

java - 我们可以有一个弱线程吗?

java - 无需 web.xml 注册 Olingo servlet

PHP 警告 : session_start(): Cannot start session when headers already sent

php - 如何将文本转换为 SVG 路径?

android - 什么类型的谷歌地图 api 用于搜索特定地点,如医院