Reputation: 153
I am trying to get values from mysql and inserting them in sqlite database.But i am getting error in json index out of range.
Here is my code. I am getting json parsing Exception:
protected void onPostExecute(Void v)
{
try{
Database1 db=new Database1(getApplicationContext());
JSONArray jArray = new JSONArray(result);
for(int i=0;i<=jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
// list1.add(json_data.getString("id"));
int s=json_data.getInt("id");
j=json_data.getString("name");
db.open();
db.inserttable1(s,j);
Toast.makeText(getBaseContext(),s+j,Toast.LENGTH_LONG).show();
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
Upvotes: 0
Views: 1251
Reputation: 43738
Look here: for(int i=0;i<=jArray.length();i++)
The loop will still execute when i=jArray.length()
which is once too often.
Change the loop to
for(int i=0;i<jArray.length();i++)
Upvotes: 3
Reputation: 787
Change your for loop to below code
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
// list1.add(json_data.getString("id"));
int s=json_data.getInt("id");
j=json_data.getString("name");
db.open();
db.inserttable1(s,j);
Toast.makeText(getBaseContext(),s+j,Toast.LENGTH_LONG).show();
}
Upvotes: 1