I have an arraylist containing multiple rows of data I wish to pass from android to a PHP server where it is displayed. I placed the arraylist contents in a JSON object which I pass on to a name-value-pair list before parsing.
My problem is when i output the value of the recieved JSON. It only displays the last of records.
PHP CODE:
<?php
if($_POST)
{
echo "Smething was sent";
$JSON_Entry = $_POST["Entry"];
$obj = json_decode($JSON_Entry);
$i = 0;
print_r($obj);
}
?>
JAVA CODE:
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\"}"}
The output displays the last of 3 rows/records
You have to create a new JSONentry after every loop. Right now you only are overriding the last set value over and over again.
You need to create a new JSONentry
for each loop iteration, then add it to your JSONArray
.
Change your code like that:
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)));
Without being a Java specialist, but I would say you need to change this line and the following ones JSONentry.put("id", String.valueOf(entryList.get(i).getEntryId())); with something like "id[]" but again - I am not a JAVA expert, but it strongly looks like you are overriding the same values over and over and therefore only the last is caught in PHP script.
Your JSONEntry is a JSONObject. You need to make a JSONArray where you will put your different JSONEntry
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.