我有一个包含多行数据的数组列表,我希望从android传递到PHP服务器,在那里它被显示。我将数组列表的内容放在一个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)));
您必须在每个循环之后创建一个新的JSONentry。现在你只是一遍又一遍地覆盖最后的设置值。
不是Java专家,但我认为您需要更改这一行和以下几行JSONentry。put (" id ", String.valueOf (entryList.get (i) .getEntryId ()));比如"id[]"但再次-我不是一个JAVA专家,但它强烈看起来像你重写相同的值一遍又一遍,因此只有最后一个被捕获在PHP脚本。
你的JSONEntry是一个JSONObject。你需要创建一个JSONArray来存放不同的JSONEntry