views:

483

answers:

2

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();
+1  A: 

You might want to use a simpler implementation. Or, switch to using HttpClient to retrieve the data, particularly using their ResponseHandler pattern.

CommonsWare
Thanks. Can I retrieve binary data too using HttpClient?
Levara
Yes, though there is no built-in `ResponseHandler` for that. I wrote one once for a consulting contract -- it was about six or so lines of code.
CommonsWare
A: 

I tested your code on both J2SE and Android and worked fine. I added a few lines to compare results for J2SE:

System.out.println("ITERATIONS: " + iterations);
System.out.println("LEN: " + stranica.length());
System.out.println("LAST 50 chars: "
        + stranica.substring(stranica.length() - 50, stranica
                .length()));

FileWriter fw = new FileWriter("/tmp/tmp-j2se.txt");
fw.write(stranica);
fw.close();

And Android:

System.out.println("ITERATIONS: " + iterations);
System.out.println("LEN: " + stranica.length());
System.out.println("LAST 50 chars: "
        + stranica.substring(stranica.length() - 50, stranica
                .length()));

FileOutputStream fos = openFileOutput("tmp-and.txt",
        Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);

System.out.println(getFileStreamPath("tmp-and.txt")
        .getAbsolutePath());

fos.write(stranica.getBytes());
fos.close();

I compared both files and were identical, but what puzzled me was the length returned by String on both platforms did not match:

J2SE:

LEN: 22479

Android:

05-22 20:28:22.733: INFO/System.out(455): LEN: 22433

However the size of the file obtained on Android platform also had a length of 22479 bytes. The only explanation I can find withouth further investigation is that some encoding (maybe line/ending) translations are being done transparently.

Back to the point of your question your code seems (and proved) correct. On which Android Platform version / Hardware are u testing your code?

Fernando Miguélez
Thank you very much. You helped me a lot. What a noob mistake...I checked my code again after reading your post. Problem was that Debug screen in Eclipse does not show whole string, but it seems that it shows only first 16k characters or so. So I thought that it doesnt append that last iteration. btw Now i have an example of how to write to file. Thanks again. :PI'm working Eclipse, Emulator 1.6 and 2.1.
Levara
Nice that the answer helped you
Fernando Miguélez