Using the code provided by the official developer website istn’t always the best idea… I wanted to download data from an interface website in the background so I used the HTTPDownloadTask from the official site and customised it so it fits my needs.
The result was this:
It actually did download my data but in the end I got tons of questionmarks…
The problem was that they used a buffer which of course can be full and kills my GSON Objekt in the onPostExecute-Method…
10-17 18:38:11.443: D/debug(13532): {"ident":"50572","ts":"24.05.2011 07:40","content":"Bin ich der einzige der dachte, beim Film 1+1=10 geht es um Nerds und nicht darum, dass 2 Schauspieler die 10 Hauptrollen spielen?","rating":"2044"}, 10-17 18:38:11.443: D/debug(13532): {"ident":"28685","ts":"20.12.2009 08:40","content":" ich wohne jetzt hier fast 10 monate und habe heute endlich mal meinen nachbarn von nebenan kennengelernt[newline] Glückwunsch [newline] in der videothek... Hat sich 7 pornos ausgeliehen[newline] lol","rating":"4787"}, 10-17 18:38:11.443: D/debug(13532): {"ident":"12820���������������������������������������������������������������������������������������������������
The solution is this piece of code:
It doesn’t use any buffers so they cannot be full 🙂
URL url; InputStream is = null; BufferedReader br; String line; try { url = new URL(urlStr); is = url.openStream(); // throws an IOException br = new BufferedReader(new InputStreamReader(is, "iso-8859-1")); while ((line = br.readLine()) != null) { result += line; } } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException ioe) { // nothing to see here } } Log.d("Downloaded", result); return result;