2012-10-18 34 views
0

我試圖從RSS提要中獲取數據以將其顯示爲片段(11片段),但每次我傳遞一個新片段的UI塊幾秒鐘,因爲它正在提取數據,所以我嘗試使用asyncTask在後臺執行此操作,但似乎不起作用。使用asyncTask和線程獲取數據

public class AndroidSaxFeedParser extends AsyncTask<String, Long, ArrayList<Article>>{ 

String dt; 
String bb; 
String nameCat; 
RootElement root; 
Element item; 

static final String RSS = "rss"; 
static final String FEED = "feed"; 
static final String ENTRY = "entry"; 
static final String CHANNEL = "channel"; 
static final String PUB_DATE = "pubDate"; 
static final String DESCRIPTION = "description"; 
static final String LINK = "link"; 
static final String TITLE = "title"; 
static final String ITEM = "item"; 
static final String CATEGORY = "category"; 
static final String DURATION = "itunes:duration"; 

ArrayList<Article> rssItems = new ArrayList<Article>(); 

public URL feedUrl; 

Context mContext; 

public AndroidSaxFeedParser(Context context) 
{ 
    mContext = context; 

} 

// ProgressDialog pd = new ProgressDialog(mContext); 

@Override 
protected void onPostExecute(ArrayList<Article> result) { 
//  pd.dismiss();  
    super.onPostExecute(result); 
} 

@Override 
protected void onPreExecute() { 
//  ProgressDialog.show(mContext, "", "Chargement..."); 
    super.onPreExecute(); 
} 

@Override 
protected ArrayList<Article> doInBackground(String... params) { 

    try { 
     feedUrl = new URL(params[0]); 
    } catch (MalformedURLException e1) { 
     e1.printStackTrace(); 
    } 

    final Article currentRssItem = new Article(); 
    root = new RootElement(RSS); 
    Element channel = root.getChild(CHANNEL); 
    item = channel.getChild(ITEM); 

    item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){ 
     public void end(String body) { 
      currentRssItem.setTitle(body); 
      Log.i("Title Article", " "+currentRssItem.getTitle()); 
     } 
    }); 

    item.getChild(CATEGORY).setEndTextElementListener(new EndTextElementListener(){ 
     public void end(String body) { 
      currentRssItem.setCategorie(body); 
      Log.i("Category Article", " "+currentRssItem.getCategorie()); 
     } 
    }); 

    item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){ 
     public void end(String body) { 
      String imgUrl, desc; 
      try {imgUrl = body.substring(body.indexOf("src=")+5,body.indexOf("\"", body.indexOf("src=")+6));} 
      catch (Exception e) 
      {imgUrl = "";} 
      try {desc=body;} 
      catch (Exception e) 
      { desc = "";} 
      currentRssItem.setImageUrl(imgUrl); 
      currentRssItem.setDescription(desc); 

      Log.i("Image URL Article", " "+currentRssItem.getImageUrl()); 
      Log.i("Description Article", " "+currentRssItem.getDescription()); 
     } 
    }); 

    item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){ 
     public void end(String body) { 
      currentRssItem.setPubDate(body); 
      Log.i("Date Article", " "+currentRssItem.getPubDate()); 
     } 
    }); 

    item.setEndElementListener(new EndElementListener(){ 
     public void end() { 
      rssItems.add(currentRssItem.copy()); 
     } 
    }); 
    try { 
     Xml.parse(feedUrl.openConnection().getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } 
    return rssItems; 
} 

我這樣在每個片段中

ArrayList<Article> feeds ; 
    try { 
     feeds=AndroidSaxFeedParser.execute(url).get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
+1

「它似乎不工作」是非常通用的。你必須總是詳細說明問題。 – waqaslam

+0

我的意思是說,每當我傳遞給一個新的片段時,用戶界面仍然會阻塞幾個次端。 –

回答

0

稱其你應該總是下載的文件,並要求XML類來分析它之前將其保存爲String

public String getXml(String url) { 
HttpGet request = new HttpGet(url); 
HttpResponse response = client.execute(request); 

// HTTP OK 200 
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
    HttpEntity entity = response.getEntity(); 
    // NOTE: Here need to set the default charset to be UTF-8 
    content = EntityUtils.toString(entity, "UTF-8"); 
    return content; 
} 
} catch (IOException e) { 
    e.printStackTrace(); 
} catch (IllegalStateException e) { 
    content = null; 
} 
} 

以這種方式,由於連接問題而不太可能中斷。另外,請告訴我們更多關於「似乎不起作用」的含義。它沒有解析XML,無法運行任務,或失敗了什麼?

而且,你不應該調用

feeds=AndroidSaxFeedParser.execute(url).get();

在片段

。你應該修改AndroidSaxFeedParser的此功能:

@Override 
protected void onPostExecute(ArrayList<Article> result) { 
    feeds = result; 
    // Do whatever you wanna do to set up things with the feeds 
} 

爲了使這個工作你必須把你的AndroidSaxFeedParser作爲一個內部類的片段。

+0

每當我傳遞給一個新的片段時,UI仍會阻止幾次調用。 –

+0

你可以請給我所有的代碼,我必須放在我的片段,我不認爲我在哪裏調用getXml()! –