I got an error "android.database.CursorIndexOutOfBoundsException: Index 20 requested, with a size of 20" and I can't understand what exactly caused it and how to fix it? Probably something wrong with c.moveToFirst() and c.moveToNext().
public class MainActivity extends AppCompatActivity {
Map<Integer, String> articleURLs = new HashMap<Integer, String>();
Map<Integer, String> articleTitles =new HashMap<Integer, String>();
ArrayList<Integer> articleIds =new ArrayList<Integer>();
SQLiteDatabase articlesDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articlesDB=this.openOrCreateDatabase("Articles", MODE_PRIVATE, null);
articlesDB.execSQL("CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY, articleId INTEGER, url VARCHAR, title VARCHAR, content VARCHAR)");
DownloadTask task = new DownloadTask();
try {
String result = task.execute("http://ift.tt/1s72oTb").get();
JSONArray jsonArray =new JSONArray(result);
articlesDB.execSQL("DELETE FROM articles");
for (int i=5; i<25;i++){
String articleId = jsonArray.getString(i);
DownloadTask getArticle = new DownloadTask();
String articleInfo = getArticle.execute("http://ift.tt/1xLQce3"+ articleId+".json?print=pretty").get();
JSONObject jsonObject= new JSONObject(articleInfo);
Log.i("jsonObject", jsonObject.toString());
String articleTitle = jsonObject.getString("title");
String articleURL = jsonObject.getString("url");
articleIds.add(Integer.valueOf(articleId));
articleTitles.put(Integer.valueOf(articleId), articleTitle);
articleURLs.put(Integer.valueOf(articleId), articleURL);
String sql = "INSERT INTO articles (articleId, url, title) VALUES (?, ?, ?)";
SQLiteStatement statement = articlesDB.compileStatement(sql);
statement.bindString(1, articleId);
statement.bindString(2, articleURL);
statement.bindString(3, articleTitle);
statement.execute();
}
Cursor c = articlesDB.rawQuery("SELECT * FROM articles",null);
int articleIdIndex = c.getColumnIndex("articleId");
int urlIndex = c.getColumnIndex("url");
int titleIndex = c.getColumnIndex("title");
c.moveToFirst();
while (c!= null){
Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
Log.i("articleUrl",c.getString(urlIndex) );
Log.i("titleTitle",c.getString(titleIndex));
c.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public class DownloadTask extends AsyncTask< String, Void, String> {
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
}
catch(Exception e){
e.printStackTrace();
}
return result;
}
}
}
Aucun commentaire:
Enregistrer un commentaire