本文介绍了BufferedReader中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从文本读取网址,然后让应用程序打开该地址,我的缓冲读者似乎被正确读取行,但readline的不断回来空

 字符串rsslink = NULL;
    InputStream为= getResources()openRawResource(R.raw.xmlsource)。
    BR的BufferedReader =新的BufferedReader(新的InputStreamReader(是));    尝试{
        而((rsslink = br.readLine())!= NULL)
        {        }
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
    字符串RSS_LINK = rsslink;    Log.d(Constants.TAG,服务启动);
    清单<&的RSSItem GT;的RSSItems = NULL;
    尝试
    {
        XMLRssParser分析器=新XMLRssParser();
        的RSSItems = parser.parse(的getInputStream(RSS_LINK));


解决方案

您将得到最后一行是空rsslink
你需要改变你的循环

  {尝试
        而((rsslink = br.readLine())!= NULL)
        {        }
    }

  {尝试
        StringBuilder的SB =新的StringBuilder();
        而((rsslink = br.readLine())!= NULL)
        {
               sb.append(rsslink);
        }
        rsslink = sb.toString();
    }

i'm trying to read a web address from a text and then have the app open that address, my buffered reader seems to be reading the lines correctly but readline keeps coming back 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));
解决方案

You will get the last line that is null rsslink.You need to change your loop

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

        }
    }

to

 try {
        StringBuilder sb=  new StringBuilder();
        while ((rsslink = br.readLine()) != null)
        {
               sb.append(rsslink);
        }
        rsslink = sb.toString();
    }

这篇关于BufferedReader中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:53