0

我已经研究了我的问题,到目前为止没有人遇到过这种情况。我正在为 Android 开发一个应用程序,我的代码抛出一个带有消息“TotalProductCount 没有值”的 JSON 异常,但前提是我不调试它。如果我在代码应该去的任何地方放置一个断点,它就可以工作。这是我的代码:

互联网服务类:

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;
    }

活动类:这是正在另一个线程上执行的可运行对象

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();  
        } 
    }

这是发生错误时发布到 UI 线程的可运行文件:

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();
         }
      }

这是一个示例 JSON 对象

{ "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" } ] }

如果我在调试中遵循它,这一切都可以正常工作,所以不可能看到出了什么问题。我希望我只是失明并且遗漏了一些明显的东西。

我希望这是有道理的。任何帮助或建议表示赞赏。谢谢

4

2 回答 2

0

你有一个js方法调用吗?如果是,您可以将代码放入回调块中。这样,您可以确保在您的方法写入该值后检索该值

于 2013-08-25T00:17:56.997 回答
0

好的,我关闭并再次打开我正在使用的android设备,看来问题已得到解决。

于 2013-08-25T12:56:49.710 回答