Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅在JSON对象中保存arraylist的最后一行_Java_Php_Android_Json_Arraylist - Fatal编程技术网

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

Java 仅在JSON对象中保存arraylist的最后一行,java,php,android,json,arraylist,Java,Php,Android,Json,Arraylist,我有一个arraylist,其中包含我希望从android传递到PHP服务器并显示的多行数据。我将arraylist内容放在一个JSON对象中,在解析之前将其传递给一个名值对列表 我的问题是当我输出接收到的JSON的值时。它只显示最后一条记录 PHP代码: <?php if($_POST) { echo "Smething was sent"; $JSON_Entry = $_POST["Entry"]; $obj = json_decode($JSON_Entry); $i =

我有一个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);
}

?>

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

您必须在每次循环后创建一个新的JSONETRY。现在,您只是一次又一次地重写上一个设置值。

不是Java专家,但我认为您需要这样做 更改此行和以下行 jsonetry.put(“id”,String.valueOf(entryList.get(i).getEntryId()); 比如“id[]”
但是再一次-我不是JAVA专家,但是看起来你在一次又一次地重写相同的值,因此在PHP脚本中只捕获最后一个值。

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

您需要为每个循环迭代创建一个新的
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)));
ArrayList entryList=db.getSalesRecords();
List postVars=new ArrayList();
JSONArray recordsJsonArray==新的JSONArray();
对于(int i=0;i
  Smething was sent{"records":"{\"total\":\"1398.0\",\"product\":\"Carlsberg\",\"id\":\"0\",\"qty\":\"2\",\"invoice\":\"2.4082015083321E13\"}"}
            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)));