http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6
这是我试图解析并显示每个项目标题的网址。
不幸的是,我一直收到此错误
05-28 22:56:08.761 27744-27744/com.tubbs.citychurchob E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
05-28 23:06:22.801 1927-1927/com.tubbs.citychurchob E/Podcast Fragment Class﹕ Couldn't open http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6
以下是所有相关代码:
播客片段类:
public class PodcastFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.podcast_fragment,container,false);
try {
RssReader rssReader = new RssReader("http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
ListView podcastItems = (ListView)v.findViewById(R.id.podcast_list);
ArrayAdapter<PodcastItem> adapter = new ArrayAdapter<PodcastItem>(getActivity(),android.R.layout.simple_list_item_1,rssReader.getItems());
podcastItems.setAdapter(adapter);
}catch (Exception e){
Log.e("Podcast Fragment Class",e.getMessage());
}
return v;
}
}
RssReader类:
public class RssReader {
private String rssFeedUrl;
public RssReader(String rssFeedUrl){
this.rssFeedUrl = rssFeedUrl;
}
public List<PodcastItem> getItems() throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
RssParseHandler handler = new RssParseHandler();
saxParser.parse(rssFeedUrl,handler);
return handler.getRssItems();
}
}
RssParseHandler类:
public class RssParseHandler extends DefaultHandler {
private List<PodcastItem> rssItems;
//Used to reference item while parsing
private PodcastItem currentItem;
//Parsing title indicator
private boolean parsingTitle;
public RssParseHandler(){
rssItems = new ArrayList<PodcastItem>();
}
public List<PodcastItem> getRssItems(){
return rssItems;
}
//Basically what this is doing is processing the entire <item> tag(at the start of it) of a particular Rss Feed List.
//It then checks to see if this is truly the start of the <item> tag or the first one
//If it is then it instantiates a New Podcast Item object
//Then it goes further through the list and once it finds <title>, it sets the boolean "parsingTitle" to true
//since it has come across that particular tag
@Override
public void startElement(String uri, String localName, String elementName, Attributes attributes) throws SAXException {
if("item".equals(elementName)){
currentItem = new PodcastItem();
} else if ("title".equals(elementName)) {
parsingTitle = true;
}
}
//Essentialy what this is doing is looking for the end <item> tag, once it has reached it, it will add
//the current PodcastItem Object to the list of "rssItems" since there is no more elements to go through.
//Then it sets the PodcastItem Object to null to start fresh at the next <item> tags.
//It then resets "parsingTitle" to false to once again start fresh at the next <item> tags.
@Override
public void endElement(String uri, String localName, String elementName) throws SAXException {
if("item".equals(elementName)){
rssItems.add(currentItem);
currentItem = null;
}else if ("title".equals(elementName)){
parsingTitle = false;
}
}
//This method checks to see whether "parsingTitle" is true or false. If it is true then it will set
// "currentItem"'s title to whatever was passed into the method. Not sure how this method works.....yet.
//YOU GOT THIS TIM
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(parsingTitle){
if(currentItem != null){
currentItem.setmPodcastTitle(new String(ch,start,length));
}
}
}
}
我还包括INTERNET权限标记。
我对XML解析很陌生,所以我有些不知所措。 任何帮助都会很棒,谢谢!
答案 0 :(得分:1)
解决了它。
对于遇到此问题的任何人,显然Android会阻止您 在主线程上进行网络呼叫。
Sooo,你必须使用 AsyncTask 在一个单独的线程上解析url:
public class PodcastFragment extends Fragment {
private View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.podcast_fragment, container, false);
GetRSSDataTask task = new GetRSSDataTask();
task.execute("http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
//RssReader rssReader = new RssReader("http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
return v;
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<PodcastItem>> {
@Override
protected List<PodcastItem> doInBackground(String... url) {
try {
//Instantiate a RSS Reader object
RssReader rssReader = new RssReader(url[0]);
//Parse the Rss Feed and get the items returned
return rssReader.getItems();
} catch (Exception e) {
Log.e("Podcast Fragment Class", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<PodcastItem> result) {
//Get a ListView from main view
ListView podcastItems = (ListView) v.findViewById(R.id.podcast_list);
//Create a List Adapter
ArrayAdapter<PodcastItem> adapter = new ArrayAdapter<PodcastItem>(getActivity(), android.R.layout.simple_list_item_1, result);
//Attach Adapter to List
podcastItems.setAdapter(adapter);
}
}
}