peon125
peon125

Reputation: 25

java android - can't download whole page html code

im trying to download a source code of a page with this code:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());

URL url;
InputStream is = null;
BufferedReader br;
String line;
List<String> list = new ArrayList<>();

try {
    url = new URL("https://csgostash.com/weapon/MAG-7");
    is = url.openStream();
    br = new BufferedReader(new InputStreamReader(is));

    while ((line = br.readLine()) != null)
        list.add(line);

    File f = new File("/data/data/abc.def.asd/cache/nwm.txt");
    FileWriter fw = new FileWriter(f, true);
    for(String x : list)
        fw.append(x +"\n");

} catch (IOException e) {
    e.printStackTrace();
}

but that doesnt download a whole code. it ends few lines before it should (i know it because, how u can see, i wrote whole content of the list to a text file). do u know any others methods to save whole website code to an array/list?

Upvotes: 0

Views: 56

Answers (1)

weston
weston

Reputation: 54791

There may not be a problem, you must close or flush a FileWriter when done:

FileWriter fw = new FileWriter(f, true);
for(String x : list)
    fw.append(x +"\n");
fw.close();

Or use try-with-resources if you have min sdk >= 19:

try (FileWriter fw = new FileWriter(f, true)) {
    for(String x : list)
        fw.append(x +"\n");
}

Otherwise you could see a partially written file.

Upvotes: 3

Related Questions