我有一個包含多行數據的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行/記錄