I'm beginner in android,write simple application to send json array to server,write this code:
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
String DATABASE_NAME = "TEMPFOOD";
String TABLE_NAME = "tempData";
try{
SQLiteDatabase mydb = openOrCreateDatabase(DATABASE_NAME, TempActivity.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
if(allrows.moveToFirst()){
do{
String Food_Name = allrows.getString(0);
String Value = allrows.getString(1);
String NOBAT = allrows.getString(2);
String TIME = allrows.getString(3);
String DATE = allrows.getString(4);
try {
obj.put("Food_Name", Food_Name)
.put("Value", Value)
.put("NOBAT", NOBAT)
.put("TIME", TIME)
.put("DATE", DATE);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(obj);
}
while(allrows.moveToNext());
}
mydb.close();
}catch(Exception e){
//Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
String jsonText = jsonArray.toString();
for example i read 4 record from sqlite
,save into the jsonArray
latest record,why?how can i solve that?thanks.