Question

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

Was it helpful?

Solution

try it -

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

OTHER TIPS

try use HttpGet to handle http GET connection (and HttpPost to handle http POST)

and make sure to always close the stream when doesn't needed.

here simple code using httpget to get String from server:

 private void executeRequest(HttpUriRequest request)
{
    HttpClient client = new DefaultHttpClient();

    HttpResponse httpResponse;
    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        System.out.println(responseCode + ":" +message);

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e)  {
        client.getConnectionManager().shutdown();
        Toast.makeText(null, "Error", Toast.LENGTH_LONG);
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        Toast.makeText(null, "Error", Toast.LENGTH_LONG);
    } catch (OAuthMessageSignerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

you can call the function like this:

HttpGet request = new HttpGet(url);

executeRequest(request);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top