androidexceptionbufferedreaderioexceptionhtc-android

IOException BufferedInputStream is closed in HTC


I got an IOException when execute BufferedReader.readLine(), in line while ((line = br.readLine()) != null) {) of the complete code below. The Exception.getMessage() returns BufferedInputStream is closed.

It only happens in HTC device, when I use Sony Ericsson XPERIA it doesn't happen.

My complete code:

public static String downloadString(String url) throws MalformedURLException, IOException {     
    InputStream is = downloadStream(url);

    //Convert stream into String
    String line;
    BufferedReader br = new BufferedReader(new InputStreamReader(is), 4096);            
    StringBuilder sb =  new StringBuilder();
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    br.close();
    return sb.toString();
}

public static InputStream downloadStream(String url) throws MalformedURLException, IOException {
    return connection(url).getInputStream();
}

private static HttpURLConnection connection(String s_url) throws MalformedURLException, IOException {
    URL url = new URL(s_url);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    return urlConnection;
}

How to make a better downloadString method that works on every device?

Thanks in advance. Really appreciate your answers


Solution

  • try it -

    public static String getContent(String url) throws Exception {
          return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next());
    }