我创建了一个应用程序。它在Android 2.2上运行良好。当我使用API高于2.2的模拟器时,它运行正常,但某些下载代码没有执行。它没有在SQLite中存储任何数据。所有功能在Android 2.2上都能正常运行。是我的代码还是模拟器上的错误?
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
这是我的代码
helper=new DBHelper(this);
try
{
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TAG);
db=helper.getReadableDatabase();
Cursor c=db.query(DBHelper.Old_Data, new String [] {DBHelper.Old_id,DBHelper.OLD_NUM}, DBHelper.Old_id+"=?",new String [] {1+""}, null, null, null);
if(c != null)
{
c.moveToFirst();
}
String strIndex = c.getString(1);
int index = Integer.parseInt(strIndex);
c.close();
i=0;
Log.d("Index , Length ",index + " , " + nl.getLength());
for (i = index; i < nl.getLength(); i++)
{
Element e = (Element) nl.item(i);
ContentValues val = new ContentValues();
val.put(DBHelper.TEST, parser.getValue(e, TEST));
val.put(DBHelper.FILE, parser.getValue(e, FILE));
val.put(DBHelper.TEST_NO, parser.getValue(e, TEST_NO));
val.put(DBHelper.FILE_URL, parser.getValue(e, EXAM_URL));
db=helper.getWritableDatabase();
db.insert(DBHelper.EXAM_DATA, null, val);
db.close();
}
if(i != index)
{
db=helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.OLD_NUM, i);
db.update(DBHelper.Old_Data, values, DBHelper.Old_id+"=?", new String [] {1+""});
db.close();
}
else
{
Toast.makeText(UserProfile.this, "No Exam Available", Toast.LENGTH_LONG).show();
}
}
catch (NullPointerException ne)
{
Toast.makeText(this, "NPE", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(this, "E", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:2)
如果您执行的任务类似解析/下载数据,请使用asynctask.Download或解析AsyncTask doinbackground方法中的数据