cannot download all the characters from a url in android
I have to download a certain json file and then store it as a text file in my application. For this i am using the following code:
public void readAndStoreFile() throws Exception {
try {
URL url = new URL(address);
URLConnection con = url.openConnection();
int length = con.getContentLength();
if (length > 0) {
Log.i("lenght of bytes received", "" + length);
InputStream fis = con.getInputStream();
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bf = new BufferedReader(isr);
String content = new String();
String line = null;
while ((line = bf.readLine()) != null) {
content +开发者_运维技巧= line;
}
FileOutputStream fos = c.openFileOutput(filename,
Context.MODE_PRIVATE);
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(
fos));
bfw.write(content);
} else
throw new NegativeArraySizeException();
} catch (Exception e) {
throw e;
}
}
But for some reason the whole json file is not being downloaded to the text file. Only a certain number of characters are downloaded and written after which the downloading and writing stops. Why is that happening?
The url that i am using is http://opisop.webuildapps.com/api/shops.json
thank you in advance.
Ok, i found out the answer , it was because of the 8k buffer capacity that only 8192 bytes were being read. I stripped out all the buffers and went to reading bytes using the inputstreamreader and it all worked out fine.
精彩评论