Saturday, 24 August 2013

Android JSON Object getDouble only working in Debug

Android JSON Object getDouble only working in Debug

I have researched my problem and so far no one is experiencing this. I am
developing an app for Android, my code is throwing a JSON Exception with
the message "No Value For TotalProductCount" but only if I do not debug
it. If I put a break point in anywhere the code should go, it works. Here
is my code:
InternetServices class:
public static ArrayList<ShoppingCart> GetItemFromBarcode(String barcode,
String sessionKey, String shop) throws ClientProtocolException,
IOException, JSONException {
ArrayList<ShoppingCart> shoppingCart = new ArrayList<ShoppingCart>();
if (shop.equals("Tesco")) {
String URL =
"https://secure.techfortesco.com/groceryapi/restservice.aspx?command=PRODUCTSEARCH&page=1&sessionkey="
+ sessionKey + "&searchtext=" + barcode;
_client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
HttpResponse r = _client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject items = new JSONObject(data);
if (items.getDouble("TotalProductCount") == 1) {
JSONArray products = items.getJSONArray("Products");
JSONObject item = products.getJSONObject(0);
shoppingCart.add(ConvertJSONtoShoppingCart(item));
}
else if (items.getDouble("TotalProductCount") == 0) {
//No product found
ShoppingCart error = new ShoppingCart();
error.setFailure(R.string.ShoppingItemReviewDialog_barcodeMessage);
shoppingCart.add(error);
}
else {
//More than one item found
int productCount = (int)
items.getDouble("PageProductCount");
JSONArray products = items.getJSONArray("Products");
for (int i=0; i < productCount; i++) {
JSONObject item = products.getJSONObject(i);
shoppingCart.add(ConvertJSONtoShoppingCart(item));
}
}
}
else {
//connecting to internet failed
ShoppingCart error = new ShoppingCart();
error.setFailure(R.string.ShoppingItemReviewDialog_loginMessage);
shoppingCart.add(error);
}
}
return shoppingCart;
}
public static ShoppingCart ConvertJSONtoShoppingCart(JSONObject item)
throws ClientProtocolException, IOException, JSONException {
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setItem(item.getString("Name"));
shoppingCart.setImage(item.getString("ImagePath"));
shoppingCart.setPrice(item.getDouble("Price"));
String temp = item.getString("OfferPromotion");
String offerString = temp.replaceAll("£", "£");
shoppingCart.setOffer(offerString);
//TODO find out how to receive the offer ID from the Tesco API
//shoppingCart.setOfferID(item.getString("OfferID"));
shoppingCart.setOfferID(item.getString("Name"));
String offer = item.getString("OfferValidity");
if (!offer.equals("") && !offer.equals(null)){
shoppingCart.setOfferStart(GetStartDateFromString(offer));
shoppingCart.setOfferEnd(GetEndDateFromString(offer));
}
return shoppingCart;
}
Activity Class: this is the runnable that is being executed on another thread
public void run() {
synchronized (t) {
if (sessionKey != null && sessionKey != "") {
try {
potentialItems =
InternetServices.GetItemFromBarcode(barcodeValue,
sessionKey, theShoppingTrip.getShop());
} catch (ClientProtocolException e) {
ErrorOnThread errorOnThread = new
ErrorOnThread(e.getMessage(), this);
handler.post(errorOnThread);
e.printStackTrace();
} catch (IOException e) {
ErrorOnThread errorOnThread = new
ErrorOnThread(e.getMessage(), this);
handler.post(errorOnThread);
e.printStackTrace();
} catch (JSONException e) {
ErrorOnThread errorOnThread = new
ErrorOnThread(e.getMessage(), this);
handler.post(errorOnThread);
e.printStackTrace();
}
}
else {
}
}
handler.post(returnRes);
synchronized (t)
{
t.interrupt();
}
}
and this is the runnable being posted to the UI thread when an error occurs:
private static class ErrorOnThread implements Runnable {
private final String errorMessage;
private final Context context;
ErrorOnThread(final String message, Context context) {
this.errorMessage = message;
this.context = context;
}
public void run() {
Toast toast = Toast.makeText(context, errorMessage,
Toast.LENGTH_LONG);
toast.show();
}
}
Here is an example JSON Object
{ "StatusCode": 0, "StatusInfo": "Processed and Logged OK", "PageNumber":
1, "TotalPageCount": 1, "TotalProductCount": 1, "PageProductCount": 1,
"Products": [ { "BaseProductId": "51644502", "EANBarcode":
"5000462001015", "CheaperAlternativeProductId": "",
"HealthierAlternativeProductId": "", "ImagePath":
"http://img.tesco.com/Groceries/pi/015/5000462001015/IDShot_90x90.jpg",
"MaximumPurchaseQuantity": 99, "Name": "Tesco Still Water 2Ltr",
"OfferPromotion": "", "OfferValidity": "", "OfferLabelImagePath": "",
"Price": 0.45, "PriceDescription": "£0.02 each", "ProductId": "258016425",
"ProductType": "QuantityOnlyProduct", "UnitPrice": 0.023, "UnitType":
"100ml" } ] }
Again this all works perfectly fine if I follow it in debug so it is
impossible to see what is going wrong. I am hoping that I am just being
blind and am missing something obvious.
I hope this makes sense. Any help or suggestions is appreciated. Thank you

No comments:

Post a Comment