基本上,我想更新我的应用程序数据库从网页获取数据...我已经写了这个类来处理我的目标:
private class updateDatabase extends AsyncTask<Void, Void, String> {
String html, minerals, url;
List<String> allMinerals;
boolean alreadyHas = false;
int isFavorite;
@Override
protected String doInBackground(Void... params) {
url = "http://minerais.cf/app.php";
allMinerals = dbHandler.getAllMineraisNames("external");
int i, j;
try {
org.jsoup.nodes.Document doc = Jsoup.connect(url).get();
html = doc.toString();
minerals = Jsoup.parse(html).text();
} catch (IOException e) {
e.printStackTrace();
}
String[] elemento = minerals.split("959");
for(i=0; i<elemento.length-1; i++){
alreadyHas = false;
String[] dados = elemento[i].split("858");
for(j=0; j<allMinerals.size()-1; j++){
if(dados[0].equals(allMinerals.get(j))){
alreadyHas = true;
if(dbHandler.isFavorite(allMinerals.get(j))){
isFavorite = 1;
}
else{
isFavorite = 0;
}
}
}
if(alreadyHas){
Mineral updating = new Mineral(
i,
dados[0],
dados[1],
dados[2],
dados[3],
dados[4],
dados[5],
dados[6],
dados[7],
dados[8],
dados[9],
dados[10],
dados[11],
dados[12],
dados[13],
dados[14],
dados[15],
dados[16],
dados[17],
isFavorite
);
dbHandler.updateMineral("external", updating);
}
else{
Mineral adding = new Mineral(
allMinerals.size(),
dados[0],
dados[1],
dados[2],
dados[3],
dados[4],
dados[5],
dados[6],
dados[7],
dados[8],
dados[9],
dados[10],
dados[11],
dados[12],
dados[13],
dados[14],
dados[15],
dados[16],
dados[17],
isFavorite
);
dbHandler.addMineral("external", adding);
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
loadList("");
}
@Override
protected void onPreExecute() {
}
}
DbHandler.java:
// Getting All Minerais
public List<String> getAllMineraisNames(String table) {
List<String> mineralList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT " + KEY_NOME + " FROM " + table + " ORDER BY " + KEY_FAVORITE + " DESC, " + KEY_NOME;
//String selectQuery = "SELECT * FROM " + table + " WHERE " + KEY_FAVORITE + "=1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
mineralList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// return mineral list
return mineralList;
}
// Updating single mineral
public int updateMineral(String table, Mineral mineral) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NOME, mineral.getNome());
values.put(KEY_FORMULAQUIMICA, mineral.getFormulaQuimica());
values.put(KEY_COMPOSICAO, mineral.getComposicao());
values.put(KEY_CRISTALOGRAFIA, mineral.getCristalografia());
values.put(KEY_PROPRIEDADESOPTICAS, mineral.getPropriedadesOpticas());
values.put(KEY_HABITO, mineral.getHabito());
values.put(KEY_CLIVAGEM, mineral.getClivagem());
values.put(KEY_DUREZA, mineral.getDureza());
values.put(KEY_DENSIDADERELATIVA, mineral.getDensidadeRelativa());
values.put(KEY_FRATURA, mineral.getFratura());
values.put(KEY_BRILHO, mineral.getBrilho());
values.put(KEY_COR, mineral.getCor());
values.put(KEY_ASSOCIACAO, mineral.getAssociacao());
values.put(KEY_PROPRIEDADESDIAGNOSTICAS, mineral.getPropriedadesDiagnosticas());
values.put(KEY_OCORRENCIA, mineral.getOcorrencia());
values.put(KEY_USOS, mineral.getUsos());
values.put(KEY_VARIEDADES, mineral.getVariedades());
values.put(KEY_OBSERVACOES, mineral.getObservacoes());
values.put(KEY_FAVORITE, mineral.getFavorite());
// updating row
return db.update(table, values, KEY_ID + " = ?",
new String[] { String.valueOf(mineral.getID()) });
}
boolean isFavorite (String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_MINERAIS_EXTERNAL, new String[] { KEY_FAVORITE
}, KEY_NOME + "=?",
new String[] { name }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
assert cursor != null;
int isFavorite = cursor.getInt(0);
return isFavorite==1;
}
void addMineral(String table, Mineral mineral) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NOME, mineral.getNome());
values.put(KEY_FORMULAQUIMICA, mineral.getFormulaQuimica());
values.put(KEY_COMPOSICAO, mineral.getComposicao());
values.put(KEY_CRISTALOGRAFIA, mineral.getCristalografia());
values.put(KEY_PROPRIEDADESOPTICAS, mineral.getPropriedadesOpticas());
values.put(KEY_HABITO, mineral.getHabito());
values.put(KEY_CLIVAGEM, mineral.getClivagem());
values.put(KEY_DUREZA, mineral.getDureza());
values.put(KEY_DENSIDADERELATIVA, mineral.getDensidadeRelativa());
values.put(KEY_FRATURA, mineral.getFratura());
values.put(KEY_BRILHO, mineral.getBrilho());
values.put(KEY_COR, mineral.getCor());
values.put(KEY_ASSOCIACAO, mineral.getAssociacao());
values.put(KEY_PROPRIEDADESDIAGNOSTICAS, mineral.getPropriedadesDiagnosticas());
values.put(KEY_OCORRENCIA, mineral.getOcorrencia());
values.put(KEY_USOS, mineral.getUsos());
values.put(KEY_VARIEDADES, mineral.getVariedades());
values.put(KEY_OBSERVACOES, mineral.getObservacoes());
values.put(KEY_FAVORITE, mineral.getFavorite());
// Inserting Row
db.insert(table, null, values);
db.close(); // Closing database connection
}
当我执行该函数时,它会抛出:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: cf.minerais, PID: 12076
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
at cf.minerais.DatabaseHandler.isFavorite(DatabaseHandler.java:164)
at cf.minerais.Main$updateDatabase.doInBackground(Main.java:134)
at cf.minerais.Main$updateDatabase.doInBackground(Main.java:109)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
我做错了什么?提前致谢
答案 0 :(得分:1)
提示在你的堆栈跟踪中:
gsutil cp
错误位于Caused by: android.database.CursorIndexOutOfBoundsException:
Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
at cf.minerais.DatabaseHandler.isFavorite(DatabaseHandler.java:164)
,您不是checking the result光标移动:
isFavorite()
(您的IDE中可能会发出一条警告:您忽略了if (cursor != null)
cursor.moveToFirst();
的回复;这些都值得关注。)
moveToFirst()
查询没有返回任何行(没有与名称匹配),因此没有第一行要移动,因此当您调用select
时,将抛出超出范围的索引。做类似的事情:
cursor.getInt(0)
我建议你输入一堆调试日志(或者只是在调试器中单步执行),这样你就可以看到你传递了什么输入以及它在哪里失败。