samedi 14 mars 2015

Adding items to sqlite database in for loop stops after 390

I have a for loop that cycles through a JSON array an inserts the data from each JSON object into a sqlite database, on the first run of the app alot of objects are downloaded in the first JSON, around 800.


However only the first 390 are added into the database, it seems after 390 the for loop just stops, nothing after the for loop is fired and the method just finishes.



protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Long", Long + ""));
params.add(new BasicNameValuePair("Lat", Lat + ""));
//params.add(new BasicNameValuePair("LastTime", lastUpdated));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(CFG.getLocationsURL,
"POST", params);

// check for success tag
try {
Log.d("com.zpwebsites.whatsintown", json.toString());
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
mydb.clearLocations();
locations.clear();
// Getting Array of Products
JSONArray products = json.getJSONArray("lists");
int productlenght = products.length();
Log.d("position length", "" + productlenght);
for (int a = 0; a < productlenght; a++) {
JSONObject c = products.getJSONObject(a);
Log.d("A value", "" + a);
// Storing each json item in variable
String name = c.getString("Name");
String desc = c.getString("Decription");
String url = c.getString("URL");
double longitude = c.getDouble("Longitude");
double Lat = c.getDouble("Lat");
String Town = c.getString("Town");
String Type = c.getString("Type");
String Premium = c.getString("Premium");
BuildingLocation map = new BuildingLocation();
map.setName(name);
map.setDesc(desc);
map.setURL(url);
map.setLong(longitude);
map.setLat(Lat);
map.setTown(Town);
map.setType(Type);
map.setPremium(Premium);
// adding HashList to ArrayList
mydb.insertLocation(map);
}
JSONObject c = products.getJSONObject(400);
Log.d("Item 400", c.toString());

}
} catch (JSONException e) {

/*parent.runOnUiThread(new Runnable() {
public void run() {
CharSequence text = "Sorry, we are having some small technical issues at the moment. Please try again shortly";
int duration = Toast.LENGTH_LONG;
Toast.makeText(parent.getBaseContext(), text, duration).show();
}
});*/

}

return null;
}


each item is added to the database but Log.d("A value", "" + a); stops at 390 and



JSONObject c = products.getJSONObject(400);
Log.d("Item 400", c.toString());


is not fired.


my database handler:



public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "locations.db";

private HashMap hp;
SQLiteDatabase db;

public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE LocationInfo (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Name TEXT, Description TEXT, URL TEXT, Longitude TEXT, Lat TEXT, Town TEXT, Type TEXT, Premium TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS LocationInfo");
db.execSQL("DROP TABLE IF EXISTS LocationType");
onCreate(db);
}

public void clearLocations(){
db.execSQL("delete from LocationInfo");
}

public boolean insertLocation (BuildingLocation place) {
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Name", place.getName());
contentValues.put("Description", place.getDecription());
contentValues.put("URL", place.getURL());
contentValues.put("Longitude", place.getLong());
contentValues.put("Lat", place.getLat());
contentValues.put("Town", place.getTown());
contentValues.put("Type", place.getType());
contentValues.put("Premium", place.getPremium());
db.insert("LocationInfo", null, contentValues);
Log.d("Database", "Inserted: " + place.getName());
db.close();
return true;
}
}


and log cat outputs:



3-14 16:58:40.036 13538-13646/com.zpwebsites.whatsintown D/Database﹕ Inserted: Roald Dahl Plass 03-14 16:58:40.038 13538-13646/c9xeeom.zpwebsites.whatsintown D/A value﹕ 390 03-14 16:58:40.101 13538-13538/com.zpwebsites.whatsintown D/local﹕ number of locations added 390 03-14 16:58:40.235 13538-13538/com.zpwebsites.whatsintown I/art﹕ Explicit concurrent mark sweep GC freed 48828(2MB) AllocSpace objects, 5(1807KB) LOS objects, 35% free, 29MB/45MB, paused 1.353ms total 46.911ms 03-14 16:58:40.313 13538-13538/com.zpwebsites.whatsintown I/art﹕ Explicit concurrent mark sweep GC freed 23063(1100KB) AllocSpace objects, 1(32KB) LOS objects, 35% free, 28MB/44MB, paused 860us total 38.425ms 03-14 16:58:40.553 13538-13538/com.zpwebsites.whatsintown I/art﹕ Explicit concurrent mark sweep GC freed 3157(107KB) AllocSpace objects, 6(196KB) LOS objects, 35% free, 29MB/45MB, paused 1.050ms total 35.041ms 03-14 16:58:40.615 13538-13538/com.zpwebsites.whatsintown I/art﹕ Explicit concurrent mark sweep GC freed 3144(98KB) AllocSpace objects, 3(98KB) LOS objects, 35% free, 28MB/44MB, paused 703us total 34.688ms 03-14 16:58:40.678 13538-13538/com.zpwebsites.whatsintown I/art﹕


but from the log it just seems to stop


Aucun commentaire:

Enregistrer un commentaire