Java 为什么SAXParser不能打开URL?

Java 为什么SAXParser不能打开URL?,java,android,xml,sax,Java,Android,Xml,Sax,这就是我试图解析并显示每个项目标题的url 不幸的是,我一直收到这个错误 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 sensitiv

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

不幸的是,我一直收到这个错误

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;
}
}
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();


}
}
公共类PodcastFragment扩展了片段{
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(右布局。播客_碎片,容器,假);
试一试{
RssReader RssReader=新RssReader(“http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
ListView播客项目=(ListView)v.findViewById(R.id.podcast_列表);
ArrayAdapter=newArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,rssReader.getItems());
设置适配器(适配器);
}捕获(例外e){
e(“播客片段类”,e.getMessage());
}
返回v;
}
}
RssReader类

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;
}
}
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();


}
}
公共类RssReader{
私有字符串rssFeedUrl;
公共RssReader(字符串rssFeedUrl){
this.rssFeedUrl=rssFeedUrl;
}
public List getItems()引发异常{
SAXParserFactory=SAXParserFactory.newInstance();
SAXParser SAXParser=factory.newSAXParser();
RssParseHandler=新的RssParseHandler();
parse(rssFeedUrl,handler);
return handler.getRssItems();
}
}
RssParseHandler类

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;
}
}
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扩展了DefaultHandler{
私有列表站点;
//用于在分析时引用项
私有PodcastItem当前项;
//解析标题指示符
私有布尔parsingtile;
公共RssParseHandler(){
rssItems=newarraylist();
}
公共列表getRssItems(){
返回rssItems;
}
//基本上,这是在处理特定Rss提要列表的整个标记(在它的开头)。
//然后检查这是否真的是标记的开始还是第一个
//如果是,则实例化一个新的播客项对象
//然后它进一步遍历列表,一旦找到,就会将布尔值“parsingTitle”设置为true
//因为它遇到了那个特殊的标签
@凌驾
public void startElement(字符串uri、字符串localName、字符串elementName、属性)引发SAXException{
如果(“项”。等于(元素名称)){
currentItem=新的PodcastItem();
}else if(“title.”等于(elementName)){
parsingTitle=真;
}
}
//基本上,这是在寻找结束标记,一旦到达它,它将添加
//当前的PodcastItem对象被添加到“rssItems”列表中,因为没有更多的元素可供使用。
//然后将PodcastItem对象设置为null,以便在下一个标记处重新开始。
//然后将“parsingTitle”重置为false,以再次在下一个标记处重新开始。
@凌驾
public void endElement(字符串uri、字符串localName、字符串elementName)引发SAXException{
如果(“项”。等于(元素名称)){
rssItems.add(当前项);
currentItem=null;
}else if(“title.”等于(elementName)){
parsingTitle=假;
}
}
//此方法检查“parsingTitle”是否为真。如果为真,则将设置它
//“currentItem”对传递到方法中的任何内容的标题。尚不确定此方法如何工作。
//你有这个提姆吗
@凌驾
公共无效字符(char[]ch,int start,int length)引发异常{
if(parsingtille){
如果(currentItem!=null){
currentItem.setmPodcastTitle(新字符串(ch,start,length));
}
}
}
}
我还包括了互联网许可标签

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

解决了它

对于任何遇到这个问题的人来说,Android显然阻止了你 在主线程上进行网络调用

因此,您必须使用AsyncTask在单独的线程上解析url,如下所示:

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));
        }
    }
}


}
公共类PodcastFragment扩展了片段{
私人观点v;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
v=充气机。充气(R.layout.podcast_碎片,容器,假);
GetRSSDataTask任务=新建GetRSSDataTask();
任务。执行(“http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
//RssReader RssReader=新RssReader(“http://fig.nowsprouting.com/citychurchofolivebranch/podcast.php?pageID=6");
返回v;
}
私有类GetRSSDataTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…url){
试一试{
//实例化RSS阅读器对象
RssReader RssReader=新的RssReader(url[0]);
//解析Rss提要并获取返回的项目
返回rssReader.getItems();
}捕获(例外e){
e(“播客片段类”,e.getMessage());
}
返回null;
}
@凌驾
受保护的void onPostExecute(列表结果){
//从主视图获取ListView
ListView播客项目=(ListView)v.findViewById(R.id.podcast_列表);
//创建一个列表适配器
ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u 1,结果);
//将适配器连接到列表
设置适配器(适配器);
}
}
}

不幸的是,我也有同样的问题,AsyncTask没有帮助