This is my json data I have created ui design using this json string but by the time of updating I am confused about this how to correctly get EditText values and how to update
{
"SingleExtras": [{
"Id": 1,
"name": "size",
"isEnabled": 1,
"values": [{
"extraId": 30,
"objId": "Gb39Rpmdl9",
"extraName": "Small",
"extraPrice": 15,
"currency": "Aed",
"isEnabled": 1
},
{
"extraId": 30,
"objId": "kknwBtS9zJ",
"extraName": "Medium",
"extraPrice": 18,
"currency": "Aed",
"isEnabled": 0
},
{
"extraId": 30,
"objId": "d5YfEGAgMt",
"extraName": "Large",
"extraPrice": 20,
"currency": "Aed",
"isEnabled": 1
}
]
},
{
"Id": 5,
"name": "Milk",
"isEnabled": 1,
"values": [{
"extraId": 30,
"objId": "8obBCWDXbh",
"extraName": "LowFatMilk",
"extraPrice": 0,
"currency": "Aed",
"isEnabled": 1
},
{
"extraId": 31,
"objId": "CC629INXuP",
"extraName": "FullFatMilk",
"extraPrice": 0,
"currency": "Aed",
"isEnabled": 1
}
]
}
],
"Extras": [{
"extraId": 5,
"objId": "PxDQX3LGU2",
"extraName": "ExtraShot",
"extraPrice": 5,
"currency": "Aed",
"isEnabled": 1
}]
}
I'm using the code below to get the modified EditText values in that
for(int i=0; i < allEds.size(); i++){
pricelist.add(allEds.get(i).getText().toString().trim());
priceidslist.add(allEds.get(i).getId());
}
This is nested array function here in loop I'm getting array index{0,1,2,0,1,2,0} like this i have all editetxt prices list by using im not able to update index position values mismatching
public void jsonarray(JSONArray arry){
try {
for (int j = 0; j < arry.length(); j++) {
JSONObject rw = arry.getJSONObject(j);
String extraId = rw.optString("extraId");
String objId = rw.optString("objId");
String extraName = rw.optString("extraName");
String extraPrice = rw.optString("extraPrice");
String currenc = rw.optString("currency");
String chksts = rw.optString("isEnabled");
if (namechklist.contains(extraName)) {
rw.put("isEnabled", "1");
}
if (nameunchklist.contains(extraName)) {
rw.put("isEnabled", "0");
}
if(allnames.contains(extraName)){
rw.put("extraPrice", pricelist.get(j));
}
Log.d("res", "jsonarray:loop "+arry.toString());
}
Log.d("res", "jsonarray: "+arry.toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
Anyone please suggest me which way is better to achieve this logic correctly
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject row = jsonArray.getJSONObject(i);
String itemid = row.optString("id");
JSONArray arry = row.getJSONArray("values");
if(i == 0){
}
else{
final int ar = arry.length();
final int tot = g + ar ;
int h = 0;
for (int j = g ; j < tot; j++) {
JSONObject rw = arry.getJSONObject(h);
String extraName = rw.optString("extraName");
String extraId = rw.optString("extraId");
if(namechklist.size() > 0){
if(namechklist.contains(extraName)){
rw.put("isEnabled","1");
}
}
if(nameunchklist.size()>0){
if(nameunchklist.contains(extraName)){
rw.put("isEnabled","0");
}
}
if(j >= arry.length()){
}else{
String gh = String.valueOf(priceidslist.get(j));
if(gh.equals(extraId)){
rw.put("extraPrice",pricelist.get(j));
}
}
h++;
}
g = arry.length();
}
}
Log.d("chk", "res:last "+jsonArray.toString());
}
JSONArray extraArray = obj.getJSONArray("Extras");
for (int i = 0; i < extraArray.length(); i++) {
JSONObject rw = extraArray.getJSONObject(i);
String extraName = rw.optString("extraName");
String extraId = rw.optString("extraId");
if(exnamechklist.size() > 0){
if(exnamechklist.contains(extraName)){
rw.put("isEnabled","1");
}
}
if(exnameunchklist.size()>0){
if(exnameunchklist.contains(extraName)){
rw.put("isEnabled","0");
}
}
}
I just changed for loop condition and changed format of getting json values now its working fine
I want to get current JSON object key from JSON array and pass it to another activity while Linear Layout OnClick function in android. My JSON array name extra_info has three objects inside the main_header there is string name of second object, I have to get the second object key pass to the another activity to get fields inside of that object from JSON array .
or else how to find index of current JSON object from JSON array
I have searched many codes but they are provided the all object key of an array.
JSON
"extra_info": [{
"type": "htmleditor",
"htmleditor_extra_title": "Key Features",
"ckeditor_content": ""
}, {
"type": "textfield",
"main_header": "Specifications",
"field_option": [{
"inside_single_title": "General",
"basic_opt1": [{
"option_name_extra": "Ideal for",
"option_desc_extra": "men"
}]
}, {
"inside_single_title": "Sandal Details",
"basic_opt1": [{
"option_name_extra": "Design",
"option_desc_extra": "Logo Detail"
}, {
"option_name_extra": "Closure",
"option_desc_extra": "Velcro"
}]
}]
}, {
"type": "textfield",
"main_header": "Sandal Details",
"field_option": [{
"inside_single_title": "General",
"basic_opt1": [{
"option_name_extra": "Sandal Details",
"option_desc_extra": "Sandal Details"
}]
}, {
"inside_single_title": "Sandal Details",
"basic_opt1": [{
"option_name_extra": null,
"option_desc_extra": null
}]
}, {
"inside_single_title": "Sandal Details",
"basic_opt1": [{
"option_name_extra": null,
"option_desc_extra": null
}]
}]
}],
Code:
specification = alertObj.getJSONArray(SPECIFICATION);
String spe = String.valueOf(specification);
System.out.println("*****JARRAY*****" + specification.length());
specificationdynamic = (LinearLayout) findViewById(R.id.specfication);
specifi = new TextView[specification.length()];
iconimg = new ImageView[specification.length()];
if (specification.length() > 0) {
Log.d("specificaiton", String.valueOf(specification.length()));
int jsoncount = Integer.parseInt(String.valueOf(specification.length()));
Log.d("specificaiton", String.valueOf(jsoncount));
for (y = 0; y < specification.length(); y++) {
specifiobj = specification.getJSONObject(y);
String speciobj = String.valueOf(specifiobj);
Log.d("specificaiton", String.valueOf(specifiobj));
Iterator key = specifiobj.keys();
while (key.hasNext()) {
k = key.next().toString();
System.out.println("Key : " + k + ", value : " +
specifiobj.getString(k));
}
// System.out.println(objects.toString());
System.out.println("-----------");
String types = specifiobj.getString("type");
if (types.equalsIgnoreCase("textfield")) {
hori = new LinearLayout(singleshooppingcart.this);
hori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
hori.setOrientation(LinearLayout.HORIZONTAL);
hori.setGravity(View.TEXT_ALIGNMENT_CENTER);
hori.setPadding(5, 15, 0, 0);
specifi[y] = new TextView(singleshooppingcart.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0 f);
params.setMargins(0, 5, 0, 0);
specifi[y].setLayoutParams(params);
String main_header = specifiobj.getString("main_header");
specifi[y].setText(main_header);
specifi[y].setTextColor(Color.parseColor("#151616"));
specifi[y].setTextSize(18);
specifi[y].setTypeface(null, Typeface.BOLD);
iconimg[y] = new ImageView(singleshooppingcart.this);
LinearLayout.LayoutParams iconimgs = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
iconimgs.setMargins(0, 0, 23, 0);
iconimg[y].setLayoutParams(iconimgs);
iconimg[y].setImageResource(R.drawable.ic_keyboard_arrow_right_black_24dp);
View v = new View(getApplicationContext());
v.setLayoutParams(new LinearLayout.LayoutParams(GetPixel(1, getApplicationContext()), LinearLayout.LayoutParams.FILL_PARENT));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
specifi[y].setText(main_header);
hori.addView(specifi[y]);
hori.addView(iconimg[y]);
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
specificationdynamic.addView(hori);
specificationdynamic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), y, Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(getApplicationContext(), "statement wrong", Toast.LENGTH_LONG).show();
}
}
Anyone Help Thanks in advance
JSONArray arraydata = responseObj.getJSONArray("messages");
JSONObject mObject;
for(int i = 0;i<arraydata.length();i++) {
mObject = arraydata.getJSONObject(i);
String MESAGE_DATA = mObject.getString("message_data");
String USER_DATA = mObject.getString("user_data");
JSONObject JSON_MSG_DATA = new JSONObject(MESAGE_DATA);
String MSG_ID = JSON_MSG_DATA.getString("message_id");
String MSG_CUSTM_ID = JSON_MSG_DATA.getString("message_custom_id");
String MSG_TEXT = JSON_MSG_DATA.getString("message_text");
String SENDER_ID = JSON_MSG_DATA.getString("sender_id");
String RECEIVER_ID = JSON_MSG_DATA.getString("receiver_id");
String MSG_DATE_CREATED = JSON_MSG_DATA.getString("date_created");
String MSG_DATE_UPDATED = JSON_MSG_DATA.getString("date_updated");
String PARTNER_ID = JSON_MSG_DATA.getString("partner_id");
String ELAPSED_TIME = JSON_MSG_DATA.getString("elapsed_time");
JSONObject JSON_USER_DATA = new JSONObject(USER_DATA);
String USER_ID = JSON_USER_DATA.getString("user_id");
String EMAIL = JSON_USER_DATA.getString("email");
String USERNAME = JSON_USER_DATA.getString("username");
String PASSWORD = JSON_USER_DATA.getString("password");
String DEVICE_ID = JSON_USER_DATA.getString("device_id");
String DEVICE_TYPE = JSON_USER_DATA.getString("device_type");
String PROFILE_IMAGE = JSON_USER_DATA.getString("profile_image");
String PHONE = JSON_USER_DATA.getString("phone");
String GENDER = JSON_USER_DATA.getString("gender");
String BIO = JSON_USER_DATA.getString("bio");
String TITLE = JSON_USER_DATA.getString("title");
String WEB_LINK = JSON_USER_DATA.getString("web_link");
String STATUS = JSON_USER_DATA.getString("status");
String USER_DATE_CREATED = JSON_USER_DATA.getString("date_created");
String USER_DATE_UPDATED = JSON_USER_DATA.getString("date_updated");
}
You can fetchdata like this.
I have created two JSON like following,
First Array:
[
{
"id":255,
"is_new":0,
"is_checked":true,
"name":"Towel Rack 650",
"is_favourite":false
},
{
"id":257,
"is_new":0,
"is_checked":true,
"name":"Towel Rod 450",
"is_favourite":false
},
{
"id":259,
"is_new":0,
"is_checked":true,
"name":"Napkin Ring - Round",
"is_favourite":false
}
]
Second Array:
[
{
"id":258,
"is_new":0,
"is_checked":false,
"name":"Towel Rod 650",
"is_favourite":true
},
{
"id":259,
"is_new":0,
"is_checked":false,
"name":"Napkin Ring - Round",
"is_favourite":true
}
]
In that I have to merge both array and also want to keep duplicate values once in final array.
I used following snippet for merging.
private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
throws JSONException {
JSONArray result = new JSONArray();
for (int i = 0; i < arr1.length(); i++) {
result.put(arr1.get(i));
}
for (int i = 0; i < arr2.length(); i++) {
result.put(arr2.get(i));
}
return result;
}
I am getting:
[
{
"id":255,
"is_new":0,
"is_checked":true,
"name":"Towel Rack 650",
"is_favourite":false
},
{
"id":257,
"is_new":0,
"is_checked":true,
"name":"Towel Rod 450",
"is_favourite":false
},
{
"id":259,
"is_new":0,
"is_checked":true,
"name":"Napkin Ring - Round",
"is_favourite":false
},
{
"id":258,
"is_new":0,
"is_checked":false,
"name":"Towel Rod 650",
"is_favourite":true
},
{
"id":259,
"is_new":0,
"is_checked":false,
"name":"Napkin Ring - Round",
"is_favourite":true
}
]
In that I am getting duplicate values of id 259 which has different values of is_checked and is_favourite which I want true value for both like:
{
"id":259,
"is_new":0,
"is_checked":true,
"name":"Napkin Ring - Round",
"is_favourite":true
}
I have also tried SparseArray but not succeed. Is there any way to do this?
Your help would be appreciated.
Alright, I wrote the code for you:
First, preparing the objects:
JSONArray arr1 = new JSONArray(),arr2 = new JSONArray(),result = new JSONArray();
JSONObject jobj = new JSONObject();
try {
jobj.put("id", "255");
jobj.put("is_new", 0);
jobj.put("is_checked", true);
jobj.put("name", "Towel Rack 650");
jobj.put("is_favourite", false);
arr1.put(jobj);
jobj = new JSONObject();
jobj.put("id", "257");
jobj.put("is_new", 0);
jobj.put("is_checked", true);
jobj.put("name", "Towel Rod 450");
jobj.put("is_favourite", false);
arr1.put(jobj);
jobj = new JSONObject();
jobj.put("id", "259");
jobj.put("is_new", 0);
jobj.put("is_checked", true);
jobj.put("name", "Napkin Ring - Round");
jobj.put("is_favourite", false);
arr1.put(jobj);
jobj = new JSONObject();
jobj.put("id", "258");
jobj.put("is_new", 0);
jobj.put("is_checked", false);
jobj.put("name", "Towel Rod 650");
jobj.put("is_favourite", true);
arr2.put(jobj);
jobj = new JSONObject();
jobj.put("id", "259");
jobj.put("is_new", 0);
jobj.put("is_checked", false);
jobj.put("name", "Napkin Ring - Round");
jobj.put("is_favourite", true);
arr2.put(jobj);
result = concatArray(arr1, arr2);
} catch (JSONException e) {
e.printStackTrace();
}
Then, the method:
private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
throws JSONException {
JSONArray result = new JSONArray();
JSONObject jobj1 = new JSONObject(),jobj2 = new JSONObject(),tmpobj = new JSONObject();
Set<String> objectsList = new HashSet<String>();
for (int i = 0; i < arr1.length(); i++) {
if(objectsList.add(arr1.getJSONObject(i).getString("id"))){
result.put(arr1.getJSONObject(i));
}
}
for (int i = 0; i < arr2.length(); i++) {
if(objectsList.add(arr2.getJSONObject(i).getString("id"))){
result.put(arr2.getJSONObject(i));
} else {
jobj1 = arr2.getJSONObject(i);
int index = 0;
for(int j = 0; j < result.length(); j++){
if(result.getJSONObject(j).getString("id").equals(jobj1.getString("id"))){
jobj2 = result.getJSONObject(j);
index = j;
}
}
tmpobj.put("id", jobj2.getString("id"));
tmpobj.put("is_new", jobj2.getInt("is_new"));
if(jobj1.getBoolean("is_checked")||jobj2.getBoolean("is_checked")){
tmpobj.put("is_checked", true);
} else {
tmpobj.put("is_checked", false);
}
tmpobj.put("name", jobj2.getString("name"));
if(jobj1.getBoolean("is_favourite")||jobj2.getBoolean("is_favourite")){
tmpobj.put("is_favourite", true);
} else {
tmpobj.put("is_favourite", false);
}
result.put(index, tmpobj);
}
}
return result;
}
You can add a SparseArray(key:id,value:each JSONObject in your JSONArray) to save your jsonobjects,every get a jsonobject,first check if its id has existed in SparseArray,if not,insert it.or you will do your logic to decide whether to insert it or update it or ignore it.
At last,if you want to get a JSONArray as return type,then add all SparseArray's values to your JSONArray result.
I am creating project for hotel to take orders in android.
In that i have to synchronize data(i am here using web-service for generating results) of sqlite of (tablet) and sqlstudio 2008 which is on server.
I am using json for it. But i want to know how to do it in Eclipse.Here is that code which is used to call web service.and while debuging it with TABLET it is showing me Error of PROCESS STOP UNEXPECTEDLY(android.process.acore)
public class JSONServices {
public static final Boolean CallService = true;
JSONHelper json = new JSONHelper();
public String MissedQueriesCount = "0";
// Function to Login A User
public SW_Login Login(String UserId, String Password) {
SW_Login loginwrapper = new SW_Login();
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.Login(UserId, Password));
} else // For Test Purpose
{
String jsonString = "{\"Service\":\"Login\",\"ResultSet\":[{\"Status\":\"1\",\"UserId\":\"21\",\"Token\":\"VJgueUxYCNaN6JGk\",\"Errorcode\":\"\",\"Errordesc\":\"\"}]}";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONArray retData = GetJSONArray(jObject, "ResultSet");
loginwrapper.Status = GetJSONElement(retData, "Status", 0);
loginwrapper.UserId = GetJSONElement(retData, "UserId", 0);
loginwrapper.Token = GetJSONElement(retData, "Token", 0);
loginwrapper.ErrCode = GetJSONElement(retData, "Errorcode", 0);
loginwrapper.ErrDesc = GetJSONElement(retData, "Errordesc", 0);
return loginwrapper;
}
// Function to Get Tables
public List<SW_Table> GetTables(String AreaId) {
JSONObject jObject = new JSONObject();
if (CallService == true) {
if (AreaId == "0") {
String strURL = JSONServiceURL.GetAllOpenOrderTables();
jObject = json.getHttpJson(strURL);
} else {
jObject = json.getHttpJson(JSONServiceURL.GetTables(AreaId));
}
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"gettables\", \"ResultSet\": [ { \"AreaID\": 1, \"TableID\": 2, \"TableDesc\": \"Table 1\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"AreaID\": 1, \"TableID\": 4, \"TableDesc\": \"Table 1\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_Table> tablewrappers = new ArrayList<SW_Table>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_Table tablewrapper = new SW_Table();
tablewrapper.Status = GetJSONElement(retData, "Status", counter);
tablewrapper.AreaID = GetJSONElement(retData, "AreaID", counter);
tablewrapper.TableID = GetJSONElement(retData, "TableID",
counter);
tablewrapper.TableDesc = GetJSONElement(retData, "TableDesc",
counter);
tablewrapper.ErrCode = GetJSONElement(retData, "Errorcode",
counter);
tablewrapper.ErrDesc = GetJSONElement(retData, "Errordesc",
counter);
tablewrappers.add(tablewrapper);
}
}
return tablewrappers;
}
// Function to Get Order Details
public List<SW_OrderDetails> GetOrderDetails(String Mode, String TableName) {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.GetOrderDetails(Mode,
TableName));
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"getorderdetails\", \"ResultSet\": [ { \"OrderID\": 7, \"OrderDevice\": \"Reception\", \"OrderIP\": \"\", \"OrderTable\": \"Table 1_1\", \"OrderDate\": \"\", \"OrderStatus\": null, \"SendPing\": \"\", \"PaymentMode\": \"\", \"CreditNumber\": \"\", \"DebitNumber\": \"\", \"CancellationReason\": \"\", \"OrderNo\": 0, \"OrderAmount\": 50.0, \"CustId\": 0, \"OrderItems\": [ { \"OrderID\": 7, \"ItemID\": 405, \"Quantity\": 2.0, \"Remarks\": \"Cold\", \"Price\": 15.0, \"Discount\": 0.0, \"SaleTax\": 0.0, \"ItemName\": \"Coffee\", \"ItemTypeId\": 47 } ], \"Status\": \"S1\", \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_OrderDetails> orderwrappers = new ArrayList<SW_OrderDetails>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_OrderDetails orderwrapper = new SW_OrderDetails();
orderwrapper.Status = GetJSONElement(retData, "Status", counter);
orderwrapper.OrderID = GetJSONElement(retData, "OrderID",
counter);
orderwrapper.OrderTable = GetJSONElement(retData, "OrderTable",
counter);
orderwrapper.OrderStatus = GetJSONElement(retData,
"OrderStatus", counter);
orderwrapper.OrderAmount = GetJSONElement(retData,
"OrderAmount", counter);
orderwrapper.ItemTypeId = GetJSONElement(retData, "ItemTypeId",
counter);
JSONArray retItemData = GetJSONArray(jObject, "OrderItems");
for (int itemcounter = 0; itemcounter <= retItemData.length() - 1; itemcounter++) {
SW_ItemDetails itemwrapper = new SW_ItemDetails();
itemwrapper.ItemID = GetJSONElement(retData, "ItemID",
counter);
itemwrapper.ItemName = GetJSONElement(retData, "ItemName",
counter);
itemwrapper.Quantity = GetJSONElement(retData, "Quantity",
counter);
itemwrapper.Remarks = GetJSONElement(retData, "Remarks",
counter);
orderwrapper.ItemDetails.add(itemwrapper);
}
orderwrapper.ErrCode = GetJSONElement(retData, "Errorcode",
counter);
orderwrapper.ErrDesc = GetJSONElement(retData, "Errordesc",
counter);
orderwrappers.add(orderwrapper);
}
}
return orderwrappers;
}
// Function to Get Order Details
public List<SW_ItemDetails> GetTableOrderDetails(String Mode, String TableId) {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.GetTableOrderDetails(
Mode, TableId));
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"gettableorder\", \"ResultSet\": [ { \"OrderID\": 3, \"ItemID\": 403, \"Quantity\": 1, \"Remarks\": \"\", \"Price\": 35.0, \"Discount\": 0.0, \"SaleTax\": 0.0, \"ItemName\": \"Hot Chocolate\", \"ItemTypeId\": 47, \"Status\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"OrderID\": 3, \"ItemID\": 405, \"Quantity\": 2, \"Remarks\": \"Cold\", \"Price\": 15.0, \"Discount\": 0.0, \"SaleTax\": 0.0, \"ItemName\": \"Coffee\", \"ItemTypeId\": 47, \"Status\": null, \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_ItemDetails> itemDetails = new ArrayList<SW_ItemDetails>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
SW_ItemDetails itemwrappertemp = new SW_ItemDetails();
itemwrappertemp.Status = GetJSONElement(retData, "Status", 0);
if (itemwrappertemp.Status.toString().trim().equals("0")) {
return null;
}
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_ItemDetails itemwrapper = new SW_ItemDetails();
itemwrapper.ItemID = GetJSONElement(retData, "ItemID", counter);
itemwrapper.ItemName = GetJSONElement(retData, "ItemName",
counter);
itemwrapper.Quantity = GetJSONElement(retData, "Quantity",
counter);
itemwrapper.Remarks = GetJSONElement(retData, "Remarks",
counter);
itemwrapper.ItemTypeId = GetJSONElement(retData, "ItemTypeId",
counter);
itemwrapper.Status = GetJSONElement(retData, "Status", counter);
itemwrapper.ErrCode = GetJSONElement(retData, "Errorcode",
counter);
itemwrapper.ErrDesc = GetJSONElement(retData, "Errordesc",
counter);
itemDetails.add(itemwrapper);
}
}
return itemDetails;
}
public void SaveOrder(String TableId, List<JSONObject> jsonarray) {
try {
Map<String, String> kvPairs = new HashMap<String, String>();
kvPairs.put("orderdetails", jsonarray.toString());
// Normally I would pass two more JSONObjects.....
if (CallService == true) {
HttpResponse re = json.doPost(JSONServiceURL
.SaveOrderDetails(TableId.toString().trim()), kvPairs);
String temp = EntityUtils.toString(re.getEntity());
if (temp.compareTo("SUCCESS") == 0) {
// Toast.makeText(this, "Sending complete!",
// Toast.LENGTH_LONG).show();
}
} else // For Test Purpose
{
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Function to Get Areas
public List<SW_Area> GetAreas() {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.SyscArea());
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"syncarea\", \"ResultSet\": [ { \"AreaId\": 1, \"AreaDesc\": \"Area 1\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"AreaId\": 2, \"AreaDesc\": \"Area 2\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"AreaId\": 3, \"AreaDesc\": \"Area 3\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_Area> areawrappers = new ArrayList<SW_Area>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_Area areawrapper = new SW_Area();
areawrapper.Status = GetJSONElement(retData, "Status", counter);
areawrapper.AreaID = GetJSONElement(retData, "AreaId", counter);
areawrapper.AreaDesc = GetJSONElement(retData, "AreaDesc",
counter);
areawrapper.ErrCode = GetJSONElement(retData, "Errorcode",
counter);
areawrapper.ErrDesc = GetJSONElement(retData, "Errordesc",
counter);
areawrappers.add(areawrapper);
}
}
return areawrappers;
}
// Function to Get Table
public List<SW_Table> GetTables() {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.SyncTable());
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"synctable\", \"ResultSet\": [ { \"AreaId\": 1, \"TableId\":1.1 , \"TableDesc\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"AreaId\": 2, \"TableId\":2.1\": \"TableDesc\":null,\"Status\": null, \"Errorcode\": null, \"Errordesc\": null }, { \"AreaId\": 3, \"TableId\":2.1\"TableDesc\": 3\", \"Status\": null, \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_Table> areawrappers = new ArrayList<SW_Table>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_Table areawrapper = new SW_Table();
areawrapper.Status = GetJSONElement(retData, "Status", counter);
areawrapper.AreaID = GetJSONElement(retData, "AreaId", counter);
areawrapper.TableID = GetJSONElement(retData, "TableID",
counter);
areawrapper.TableDesc = GetJSONElement(retData, "TableDesc",
counter);
areawrapper.ErrCode = GetJSONElement(retData, "Errorcode",
counter);
areawrapper.ErrDesc = GetJSONElement(retData, "Errordesc",
counter);
areawrappers.add(areawrapper);
}
}
return areawrappers;
}
// Function to Get ItemType
public List<SW_ItemType> GetItemTypes() {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.SyncItemType());
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"syncitemtype\", \"ResultSet\": [ {\"Status\":null, \"ItemTypeId\": 1, \"ItemTypeName\":Cold Drinks \"ItemTypeDesc\": null, \"ItemTypeCode\": null, \"Discount\":30,\"SalesTax\":12.50,\"Flag\":null,\"ImageIndex\":null,\"Errorcode\": null, \"Errordesc\": null },{\"Status\":null,\"ItemTypeId\": 2, \"ItemTypeName\":MainCourse,\"ItemTypeDesc\": null, \"ItemTypeCode\": null, \"Discount\":25,\"SalesTax\":12.50,\"Flag\":null,\"ImageIndex\":null,\"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_ItemType> areawrappers = new ArrayList<SW_ItemType>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_ItemType areawrapper = new SW_ItemType();
areawrapper.Status = GetJSONElement(retData, "Status", counter);
areawrapper.ItemTypeId = GetJSONElement(retData, "ItemTypeId", counter);
areawrapper.ItemTypeName = GetJSONElement(retData, "ItemTypeName", counter);
areawrapper.ItemTypeDesc = GetJSONElement(retData, "ItemTypeDesc", counter);
areawrapper.ItemTypeCode = GetJSONElement(retData, "ItemTypeCode", counter);
areawrapper.Discount = GetJSONElement(retData, "Discount",counter);
areawrapper.SalesTax = GetJSONElement(retData, "SalesTax",counter);
areawrapper.Flag = GetJSONElement(retData, "Flag",counter);
areawrapper.ImageIndex = GetJSONElement(retData, "ImageIndex",counter);
areawrapper.ErrCode = GetJSONElement(retData, "ErrCode",counter);
areawrapper.ErrDesc = GetJSONElement(retData, "ErrDesc",counter);
areawrappers.add(areawrapper);
}
}
return areawrappers;
}
// Function to Get Item
public List<SW_Item> GetItems() {
JSONObject jObject = new JSONObject();
if (CallService == true) {
jObject = json.getHttpJson(JSONServiceURL.SyscArea());
} else // For Test Purpose
{
String jsonString = "{ \"ResFrom\": \"syncitem\", \"ResultSet\": [ { \"Status\": 1, \"ItemId\":1, \"ItemTypeId\":2,\"ItemName\":Jaljira,\"ItemDesc\":null,\"Price\":20,\"Active\":null,\"ItemCode\":null, \"Errorcode\": null, \"Errordesc\": null },{ \"Status\": 1, \"ItemId\":2, \"ItemTypeId\":3,\"ItemName\":NimbuPani,\"ItemDesc\":null,\"Price\":15,\"Active\":null,\"ItemCode\":null, \"Errorcode\": null, \"Errordesc\": null } ] }";
try {
jObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<SW_Item> areawrappers = new ArrayList<SW_Item>();
JSONArray retData = GetJSONArray(jObject, "ResultSet");
if (retData != null) {
for (int counter = 0; counter <= retData.length() - 1; counter++) {
SW_Item areawrapper = new SW_Item();
areawrapper.Status = GetJSONElement(retData, "Status", counter);
areawrapper.ItemId = GetJSONElement(retData, "ItemId", counter);
areawrapper.ItemTypeId = GetJSONElement(retData, "ItemTypeId",counter);
areawrapper.ItemName = GetJSONElement(retData, "ItemName", counter);
areawrapper.ItemDesc = GetJSONElement(retData, "ItemDesc", counter);
areawrapper.Price = GetJSONElement(retData, "Price", counter);
areawrapper.Active = GetJSONElement(retData, "Active", counter);
areawrapper.ItemCode = GetJSONElement(retData, "ItemCode", counter);
areawrapper.ErrCode = GetJSONElement(retData, "Errorcode",counter);
areawrapper.ErrDesc = GetJSONElement(retData, "Errordesc",counter);
areawrappers.add(areawrapper);
}
}
return areawrappers;
}
private String Encrypt(String password) {
String toEnc = password; // Value to encrypt
MessageDigest mdEnc = null;
try {
mdEnc = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Encryption algorithm
mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
if (md5.length() < 32)
md5 = "0" + md5;
return md5;
}
/* **************************************** */
/* Private Methods To Extract JSON Contents */
/* **************************************** */
private JSONArray GetJSONArray(JSONObject obj, String arrayName) {
JSONArray retData = null;
try {
retData = obj.getJSONArray(arrayName);
} catch (JSONException e) {
e.printStackTrace();
}
return retData;
}
private String GetJSONElement(JSONArray jsonArr, String Element, int index) {
String element = "";
try {
element = jsonArr.getJSONObject(index).getString(Element)
.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return element;
}
private String GetJSONString(JSONObject jObj, String Element) {
String element = "";
try {
element = jObj.getString(Element).toString();
} catch (JSONException e) {
e.printStackTrace();
}
return element;
}
private JSONObject GetJSONObject(JSONArray jsonArr, int index) {
JSONObject retData = null;
try {
retData = jsonArr.getJSONObject(index);
} catch (JSONException e) {
e.printStackTrace();
}
return retData;
}
}