0
我使用AsyncTask從RSS提要和DOM解析器和ArrayAdapter獲取數據。RSS需要太長時間才能在android中加載
我注意到RSS Feed在屏幕上加載和顯示數據的時間太長,起初我以爲因爲文章數量很大,但是當我限制for循環只讀3篇文章時,它仍然花費太長時間展示文章!
下面是完整的代碼我使用的解析數據和主要活動
主要業務代碼:
public class NewsMainActivity extends Activity implements OnItemClickListener {
/** Called when the activity is first created. */
ListView listview;
List<NewsBean> arrayList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
initComponents();
if (Utils.isNetworkAvailable(this)) {
new MyRssReadTask()
.execute("feed_URL");
} else {
Toast.makeText(this, "Please check your internet connection", Toast.LENGTH_SHORT)
.show();
}
}
private void initComponents() {
listview = (ListView) findViewById(android.R.id.list);
listview.setOnItemClickListener(this);
}
class MyRssReadTask extends AsyncTask<String, Void, Void> {
ProgressDialog waitingDialog;
@Override
protected void onPreExecute() {
waitingDialog = new ProgressDialog(NewsMainActivity.this);
waitingDialog.setMessage("Loading articles, Please wait...");
waitingDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(String... urls) {
arrayList = new NewsParser().getData(urls[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
waitingDialog.dismiss();
setDataToListView();
super.onPostExecute(result);
}
}
protected void setDataToListView() {
if (null != arrayList && arrayList.size() != 0) {
NewsRowAdapter objNewsRowAdapter = new NewsRowAdapter(
NewsMainActivity.this, R.layout.row, arrayList);
listview.setAdapter(objNewsRowAdapter);
} else {
showToast("There was an error to get data, please try again later.");
}
}
void showToast(String msg) {
Toast.makeText(NewsMainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final NewsBean objBean = (NewsBean) arrayList.get(position);
Intent intent = new Intent(NewsMainActivity.this,
NewsDetail.class);
intent.putExtra("title", objBean.getTitle());
intent.putExtra("description", objBean.getDescription());
//intent.putExtra("pubdate", objBean.getPubDate());
//intent.putExtra("creator", objBean.getCreator());
intent.putExtra("link", objBean.getLink());
startActivity(intent);
}
新聞分析器
public class NewsParser {
private List<NewsBean> arrayListPasre;
public List<NewsBean> getData(String _url) {
try {
arrayListPasre = new ArrayList<NewsBean>();
URL url = new URL(_url);
URLConnection con = url.openConnection();
System.out.println("Connection is : " + con);
System.out.println("Connection InputStream is : "
+ con.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(
con.getInputStream()));
System.out.println("Reader us now : " + reader);
String inputLine;
String fullStr = "";
while ((inputLine = reader.readLine()) != null)
fullStr = fullStr.concat(inputLine + "\n");
InputStream istream = url.openStream();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
System.out.println("Builder : " + builder);
Document doc = builder.parse(istream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
System.out.println("nlist " + nList);
for (int temp = 0; temp < 3; temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
NewsBean objBean = new NewsBean();
objBean.setTitle(getTagValue("title", eElement));
objBean.setDescription(getTagValue("description", eElement));
objBean.setLink(getTagValue("link", eElement));
arrayListPasre.add(objBean);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayListPasre;
}
private String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
任何建議,我將感謝! :)
你可以更清楚地知道如何做到這一點?謝謝 – Moe 2012-08-13 01:40:22
哪裏的延誤最大?此文檔doc = builder.parse(istream); ? – 2012-08-13 09:35:40
不太確定,但是當它解析Feed文件然後標準化其元素 – Moe 2012-08-13 09:50:52