title: Android JSON Object getDouble only working in Debug
body: <p>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: </p>
  
  <p>InternetServices class:</p>
  
  <pre><code>public static ArrayList&lt;ShoppingCart&gt; GetItemFromBarcode(String barcode, String sessionKey, String shop) throws ClientProtocolException, IOException, JSONException {
          ArrayList&lt;ShoppingCart&gt; shoppingCart = new ArrayList&lt;ShoppingCart&gt;();
  
  if (shop.equals("Tesco")) {
              String URL = "https://secure.techfortesco.com/groceryapi/restservice.aspx?command=PRODUCTSEARCH&amp;page=1&amp;sessionkey=" + sessionKey + "&amp;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 &lt; 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("") &amp;&amp; !offer.equals(null)){
              shoppingCart.setOfferStart(GetStartDateFromString(offer));
              shoppingCart.setOfferEnd(GetEndDateFromString(offer));
          }
          return shoppingCart;
      }
  </code></pre>
  
  <p>Activity Class:
  this is the runnable that is being executed on another thread</p>
  
  <pre><code>public void run() {
          synchronized (t) {
              if (sessionKey != null &amp;&amp; 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();  
          } 
      }
  </code></pre>
  
  <p>and this is the runnable being posted to the UI thread when an error occurs:</p>
  
  <pre><code>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();
           }
        }
  </code></pre>
  
  <p>Here is an example JSON Object</p>
  
  <pre><code>{ "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" } ] }
  </code></pre>
  
  <p>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.</p>
  
  <p>I hope this makes sense.
  Any help or suggestions is appreciated. Thank you</p>