문제

I'm developing an app. That app needs to get the content of a simple .php URL, and save it as a String. The problem is that it is a very long String (VERY LONG) and it get's but in half. Take this link as an example:
http://thuum.org/download-dev-notes-web.php
With this code

URL notes = new URL("http://thuum.org/download-dev-notes-web.php")
BufferedReader in = new BufferedReader(new InputStreamReader(notes.openStream()));
String t = "";
while ((inputLine = in.readLine()) != null)
 t = inputLine;
fOut = openFileOutput("notes", MODE_PRIVATE);
fOut.write(t.getBytes());
// Added This \/ to see it's length when divided, and it is not nearly as much as it should be
System.out.println(t.split("\\@").length);

Can someone tell me how would I be able to download that into a String, and save it into the internal storage without it getting cut? Some why it looks like it gets only the last x digits...

도움이 되었습니까?

해결책

it seems you're overwriting your String t in every iteration of the while-loop. Try this:

    StringBuilder result = new StringBuilder();
    String inputLine = "";
    while ((inputLine = in.readLine()) != null) {
        result.append(inputLine);
    }
    fOut = openFileOutput("notes", MODE_PRIVATE);
    fOut.write(result.toString().getBytes());

It creates a mutable StringBuilder and uses the resulting (immutable) String in the write call.

edit: I also recommend to always use curly brackets to indicate end of loop bodies, ommiting those can quickly lead to bugs, just check #gotofail for a recent example ;-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top