在我的RSS閱讀器應用程序中,我試圖檢查輸入RSS網址是否有效。如果是這樣,我會將它保存在SQLite數據庫中。我的方法是使用的AsyncTask:錯誤,同時在AsyncTask中顯示Toast Android
class mAsyncTask extends AsyncTask<String, Integer, String>
{
private boolean succeed = false;
public mAsyncTask()
{
}
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Input RSS feed is not in the valid format...", Toast.LENGTH_LONG).show();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}
return "Complete doInBackground";
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (succeed == true)
{
db.addSource(new Sources("", sourceTitle, sourceUrlString));
//refresh
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
sourceUrlString
是AlertDialog輸入字符串。 db
是數據庫處理程序類。構造函數構造函數擁有3個變量:類別,標題和Feed的url,所有這些都是String。
基於LogCat,我每次嘗試在catchs
內顯示Toast時都會收到錯誤。任何想法爲什麼?另外,這是我第一次嘗試實現AsyncTask,所以你應該注意到任何低效的代碼,請讓我知道。
無法顯示'doInbackground'敬酒,因爲你不能從後臺線程更新UI和doInbackground調用在後臺線程 – Raghunandan
你不能鞋土司doInBackground –
使用的System.out.println(),因爲ü不能更新ui在asynctask – raj