用于获取和解析 xml 的 Android 应用程序
我已经为 Android 应用程序编写了用于获取和解析 xml 的代码。但我无法从另一个类调用方法 getfeed()
。
代码在下面给出的
public class RssReader extends Activity {
private RssFeed feed =null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
feed=new getFeed(Url);
}
}
另一个类下面给出
private RssFeed getFeed(String urlToRssFeed)
{
try
{
URL url= new URL(urlToRssFeed);
SAXParserFactory factory =SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader xmlreader=parser.getXMLReader();
RssHandler theRSSHandler=new RssHandler();
xmlreader.setContentHandler(theRSSHandler);
InputSource is=new InputSource(url.openStream());
xmlreader.parse(is);
return theRSSHandler.getFeed();
}
catch (Exception ee)
{
return null;
}
}
I have written code for Android Application for fetch and parse xml.but i am not able call method getfeed()
from another class.
Code is given below
public class RssReader extends Activity {
private RssFeed feed =null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
feed=new getFeed(Url);
}
}
another class given below
private RssFeed getFeed(String urlToRssFeed)
{
try
{
URL url= new URL(urlToRssFeed);
SAXParserFactory factory =SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader xmlreader=parser.getXMLReader();
RssHandler theRSSHandler=new RssHandler();
xmlreader.setContentHandler(theRSSHandler);
InputSource is=new InputSource(url.openStream());
xmlreader.parse(is);
return theRSSHandler.getFeed();
}
catch (Exception ee)
{
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 getFeed() 在您的 RssFeed 类中(确保它是公共的),则不要使用
feed=new getFeed(Url);
,而是使用:您可能想查看此 有关 Android 上 RSS 阅读的问题。
If getFeed() is in your class RssFeed (make sure it's public), then instead of
feed=new getFeed(Url);
, use:You might want to check out this question regarding RSS Reading on Android.
Url
在哪里定义的?我在列出的代码中没有看到它...Activity
执行任何操作吗?根据getFeed(Url)
的作用,您的代码似乎没有执行任何操作(除了可能解析某些 xml,但它对方法输出不执行任何操作)。也许您只是陷入了第一个问题......new
operator in front of getFeed, because that is not valid.Url
defined? I do not see it in the listed code...Activity
to do anything? Depending on whatgetFeed(Url)
does, your code does not appear to do anything (except maybe parse some xml, but it does nothing with the method output). Perhaps you are just stuck on this first issue though...由于调用方法和方法定义都在不同的类中
,因此访问说明符是
private
。私有方法不能在类外部访问。因此将访问说明符更改为 public。写
public RssFeed getFeed(String urlToRssFeed)
而不是private RssFeed getFeed(String urlToRssFeed)
谢谢
迪帕克
As the calling method and method defination both are in different class
Here the access specifier is
private
. Private methods cannot be accessed outside the class. so change the access specifer into public.write
public RssFeed getFeed(String urlToRssFeed)
instead ofprivate RssFeed getFeed(String urlToRssFeed)
Thanks
Deepak