StringBuffer wont read whole stream into a string (JAVA/Android)

Posted by Levara on Stack Overflow See other posts from Stack Overflow or by Levara
Published on 2010-05-22T16:58:51Z Indexed on 2010/05/22 17:30 UTC
Read the original article Hit count: 256

Hi all! I'm making an android program that retrieves content of a webpage using HttpURLConnection. I'm new to both Java and Android.

Problem is: Reader reads whole page source, but in the last while iteration it doesn't append to stringBuffer that last part.

Using debbuger I have determined that, in the last loop iteration, string buff is created, but stringBuffer just doesnt append it.

I need to parse retrieved content. Is there any better way to handle the content for parsing than using strings. I've read on numerous other sites that string size in Java is limited only by available heap size. I've tried with StringBuilder too.

Anyone know what could be the problem. Btw feel free to suggest any improvements to the code.

Thanks!

URL u;
    try {
        u = new URL("http://feeds.timesonline.co.uk/c/32313/f/440134/index.rss");
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestProperty("User-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)");
        c.setRequestMethod("GET");
        c.setDoOutput(true);
       c.setReadTimeout(3000);
          c.connect();
        StringBuffer stringBuffer = new StringBuffer("");
        InputStream in = c.getInputStream();
        InputStreamReader inp = new InputStreamReader(in);
        BufferedReader reader = new BufferedReader(inp);
        char[] buffer = new char[3072];
        int len1 = 0;
        while ( (len1 = reader.read(buffer)) != -1 ) 
        {
            String buff = new String(buffer,0,len1);
            stringBuffer.append(buff);
        }
        String stranica = new String(stringBuffer);
        c.disconnect();
        reader.close();
        inp.close();
        in.close();

© Stack Overflow or respective owner

Related posts about java

Related posts about android