简体   繁体   English

Java Android Json OutOfMemoryError

[英]Java Android Json OutOfMemoryError

I'm trying to decode a json string and do something with the contents. 我正在尝试解码json字符串并对其内容进行处理。 I have this code that runs whenever the user presses a button: 我有这段代码在用户按下按钮时运行:

public List<Card> readCards()
{
    List<Card> cards = new ArrayList<Card>();

    HttpReader httpReader = new HttpReader();
    httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
        @Override
        public void resultReady(String result) {
            JsonHelper jsonHelper = new JsonHelper();
            List<Card> cards = jsonHelper.getCards(result);

            for (int i = 0; i < cards.size(); i++ ) {
                cards.add(new Card(cards.get(i).getId(), cards.get(i).getNaam(), cards.get(i).getMana(), cards.get(i).getAttack(), cards.get(i).getHealth(), cards.get(i).getEffect(), cards.get(i).getZeldzaamheid(), cards.get(i).getTypeId(), cards.get(i).getSubtypeId(), cards.get(i).getClassId(), cards.get(i).isGoud()));
            }
        }
    });
    httpReader.execute("http://jsonstring.com"); //link to json-file

    return cards;
}

The getCards(result) method from the jsonHelper class is this: jsonHelper类的getCards(result)方法是这样的:

public List<Card> getCards(String jsonText) {
    List<Card> list= new ArrayList<Card>();

    try {
        JSONArray jsonArrayCards = new JSONArray(jsonText);
        for (int i = 0; i < jsonArrayCards.length(); i++) {
            JSONObject jsonObjectCard = jsonArrayCards.getJSONObject(i);    

                Card card = new Card();
                if ( jsonObjectCard.has("id")) { card.setId(jsonObjectCard.getString("id")); } else { card.setId("none"); }
                if ( jsonObjectCard.has("name")) { card.setNaam(jsonObjectCard.getString("name")); } else { card.setNaam("none"); }
                if ( jsonObjectCard.has("cost")) { card.setMana(jsonObjectCard.getInt("cost")); } else { card.setMana(0); }
                if ( jsonObjectCard.has("attack")) { card.setAttack(jsonObjectCard.getInt("attack")); } else { card.setAttack(0); }
                if ( jsonObjectCard.has("health")) { card.setHealth(jsonObjectCard.getInt("health")); } else { card.setHealth(0); }
                if ( jsonObjectCard.has("text")) { card.setEffect(jsonObjectCard.getString("text")); } else { card.setEffect(""); }
                card.setTypeId(1);
                card.setSubtypeId(1);
                card.setClassId(1);
                list.add(card);
        }
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return list;
}

After this is done I'm trying to show to the size of the returned list, which is 0 for some reason. 完成此操作后,由于某些原因,我试图显示返回列表的大小,该大小为0。

After clicking the button the first time the app freezes. 首次单击该按钮后,应用程序冻结。 Log shows a java.lang.OutOfMemoryError 日志显示java.lang.OutOfMemoryError

The json file is <200 lines which is maybe 5kb so that should not be a problem. json文件小于200行,可能为5kb,所以应该没有问题。

Any help would be much appreciated. 任何帮助将非常感激。

Your program is stuck in infinite loop. 您的程序陷入了无限循环。 You are adding cards to your cards list that keeps growing until you get out of memory 您正在将卡添加到卡列表中,该列表一直在增长,直到内存不足为止

        for (int i = 0; i < cards.size(); i++ ) {
            cards.add(new Card(cards.get(i).getId(),...
        }

Loop can never finish because you test that i < cards.size() and with each new Card added cards.size increases. 循环永远不会结束,因为您要测试i < cards.size()并在添加每张new Card增加cards.size

Your for loop is the cause of the error 您的for循环是导致错误的原因

     for (int i = 0; i < cards.size(); i++ ) 

And Using cards.size() in for loop is not a best practice. 而且在for循环中使用cards.size()并不是最佳实践。 Instead use 改为使用

     int count = cards.size();

Always use this int in for loop. 始终在for循环中使用此int。 This is more faster than the older one. 这比旧的要快。 Because In old code, every time your for loop counts for cards 因为在旧代码中, 每次 for循环都会计数卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM