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, st开发者_开发知识库ring 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();
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?
You might want to use a simpler implementation. Or, switch to using HttpClient
to retrieve the data, particularly using their ResponseHandler
pattern.
精彩评论