0

我正在尝试从文本中读取网址,然后让应用程序打开该地址,我的缓冲阅读器似乎正在正确读取行但 readline 不断返回 null

String rsslink = null;
    InputStream is = getResources().openRawResource(R.raw.xmlsource);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    try {
        while ((rsslink = br.readLine()) != null) 
        {

        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    String RSS_LINK = rsslink;

    Log.d(Constants.TAG, "Service started");
    List<RssItem> rssItems = null;
    try 
    {
        XMLRssParser parser = new XMLRssParser();
        rssItems = parser.parse(getInputStream(RSS_LINK));
4

2 回答 2

1

您将得到最后一行,即null rsslink. 你需要改变你的循环

try {
        while ((rsslink = br.readLine()) != null) 
        {

        }
    } 

 try {
        StringBuilder sb=  new StringBuilder();
        while ((rsslink = br.readLine()) != null) 
        {
               sb.append(rsslink);
        }
        rsslink = sb.toString();
    } 
于 2013-10-25T05:57:28.260 回答
0

用这个:

String rsslink = "";
InputStream is = getResources().openRawResource(R.raw.xmlsource);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;

try {
    while ((line = br.readLine()) != null) 
    {
       rsslink +=line ;
    }
} 
catch (IOException e) 
{
    e.printStackTrace();
}
String RSS_LINK = rsslink;

Log.d(Constants.TAG, "Service started");
List<RssItem> rssItems = null;
try 
{
    XMLRssParser parser = new XMLRssParser();
    rssItems = parser.parse(getInputStream(RSS_LINK));

更好的StringBufferStringBuilder

StringBuilder rsslink = new StringBuilder();
InputStream is = getResources().openRawResource(R.raw.xmlsource);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;

try {
    while ((line = br.readLine()) != null) 
    {
       rsslink.append(line);
    }
} 
catch (IOException e) 
{
    e.printStackTrace();
}
String RSS_LINK = rsslink.toString();
于 2013-10-25T06:04:42.690 回答