【问题标题】:How come SAXParser cannot open URL?为什么 SAXParser 无法打开 URL?
【发布时间】:2015-05-29 04:11:52
【问题描述】:
http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6

这是我试图解析并显示每个项目标题的 url。

不幸的是,我不断收到此错误

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 Permission 标签。

我对 XML 解析很陌生,所以我有点不知所措。 任何帮助都会很棒,谢谢!

【问题讨论】:

    标签: java android xml sax


    【解决方案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);
        }
    }
    }
    

    【讨论】:

    • 不幸的是我有同样的问题,AsyncTask 没有帮助
    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 2015-02-28
    • 2010-12-14
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多