lundi 29 décembre 2014

Android json HttpReader not retrieving anything

I'm using an SQLite database to store objects that I retrieve using a json string. I have a List<Card> cards that should fill up with objects, but doesn't.


I have set <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.xml


insertCards function that gets called when creating the database:



private void insertCards(SQLiteDatabase db) {

final List<Card> cards = new ArrayList<Card>();

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

int numberOfCards = cardsJson.size();
for (int i = 0; i < numberOfCards - 1; i++) {
cards.add(new Card(cardsJson.get(i).getId(),
cardsJson.get(i).getNaam(),cardsJson
.get(i).getMana(), cardsJson.get(i)
.getAttack(), cardsJson.get(i)
.getHealth(), cardsJson.get(i)
.getEffect(), cardsJson.get(i)
.getZeldzaamheid(), cardsJson
.get(i).getTypeId(), cardsJson.get(
i).getSubtypeId(), cardsJson.get(i)
.getClassId(), cardsJson.get(i)
.isGoud()));
}
}
});
httpReader.execute("http://eduphp.khk.be/~r0370877/EnkelExpert.json");

int numberOfCards2 = cards.size();

for (int i = 0; i < numberOfCards2 - 1; i++) {
insertCard(cards.get(i));
}

// These lines are to check whether or not cards has any records. They both get inserted in the database.
db.execSQL("INSERT INTO card (id, naam, mana, attack, health, effect, zeldzaamheid, typeId, subtypeId, classId, goud) VALUES ('CS2_188', 'Abusive Sergeant', '1', '2', '1', 'Effect', 'Common', '1', '1', '1', '0');");

if (numberOfCards2 == 0) {
db.execSQL("INSERT INTO card (id, naam, mana, attack, health, effect, zeldzaamheid, typeId, subtypeId, classId, goud) VALUES ('CS2_189', 'azeaze Sergeant', '1', '2', '1', 'Effect', 'Common', '1', '1', '1', '0');");

}
}


Those last db.execSQL() lines are examples that both get inserted into the database, even though only the first one should get inserted.


insertCard(Card card) function that gets called:



public long insertCard(Card card) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("id", card.getId());
values.put("naam", card.getNaam());
values.put("mana", card.getMana());
values.put("attack", card.getAttack());
values.put("health", card.getHealth());
values.put("effect", card.getEffect());
values.put("zeldzaamheid", card.getZeldzaamheid());
values.put("typeId", card.getTypeId());
values.put("subtypeId", card.getSubtypeId());
values.put("classId", card.getClassId());

// 'goud' is a boolean, but SQLite can't store booleans
int goud = 0;
if (card.isGoud() == true) {
goud = 1;
} else {
goud = 0;
}

values.put("goud",goud);

long id = db.insert("card", null, values);

db.close();
return id;
}


JsonHelper.java:



public class JsonHelper {

public List<Card> getCards(String jsonTekst) {
List<Card> cards = new ArrayList<Card>();

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

if (jsonObjectCard.has("collectible")) {
if (jsonObjectCard.getBoolean("collectible") == true) {
Card card = new Card();
if ( jsonObjectCard.has("id")) { card.setId(jsonObjectCard.getString("id")); } else { card.setId("geen"); }
if ( jsonObjectCard.has("name")) { card.setNaam(jsonObjectCard.getString("name")); } else { card.setNaam("GeenNaam"); }
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(""); }
if ( jsonObjectCard.has("rarity")) { card.setZeldzaamheid(jsonObjectCard.getString("rarity")); } else { card.setZeldzaamheid("Common"); }
// The following lines are hard coded
card.setTypeId(1);
card.setSubtypeId(1);
card.setClassId(1);
card.setGoud(false);
cards.add(card);
}
}
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

return cards;
}
}


So basically I'm not getting back any records from the HttpReader.


Aucun commentaire:

Enregistrer un commentaire