Archie.bpgc
Archie.bpgc

Reputation: 24012

Attempted read on closed stream

I get this exception when i scroll my ListView and fetch new data when last item in the list is reached.

This is the code to get new data:

public static String PostData(String url, String sa[][]) {

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    int n = sa.length;
    for (int i = 0; i < n; i++) {
        nameValuePairs.add(new BasicNameValuePair(sa[i][0], sa[i][1]));
    }
    HttpPost httppost;
    try {
        httppost = new HttpPost(url);
    } catch (Exception e) {
    }
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
    }
    HttpResponse response = null;
    try {
        response = client.execute(httppost);
    }catch (Exception e) {
    }
    HttpEntity entity = response.getEntity();
    try {
        is = entity.getContent();
    } catch (Exception e) {
    }
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        sb.append(reader.readLine());
        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append("\n" + line);
        }
        result = sb.toString();
    } catch (Exception e) {
        //I get Exception here saying: Attempted read on closed stream
    }finally{
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
}

the client i used is:

private static DefaultHttpClient client = getThreadSafeClient();

of

public static DefaultHttpClient getThreadSafeClient() {

    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
            mgr.getSchemeRegistry()), params);
    return client;
}

The method PostData gets new 10 items when ever we scroll to the bottom of the ListView.

The problem is, this works fine, for first 3-4 scrolls, and later i get the Exception and the new 10 items added are from the previous data(duplication).

Some times i even get this Exception:

Socket closed

in place of

Attempted read on closed stream

but i haven't used any Sockets, to try fixing it.

Edit:

Actually i have Log in every catch block, which prints the Exception. But i didn't add all those Logs in the question. The ones which i get in the LogCat are listed above.

I keep getting these lines in the Logcats info:

11-21 15:49:57.735: I/System.out(1034): close [socket][/0.0.0.0:36508]
11-21 15:49:57.735: I/System.out(1034): close [socket][/0.0.0.0:36508]
11-21 15:50:02.978: I/System.out(1034): [socket][3] connection /76.74.166.78:80(0)
11-21 15:50:03.010: I/System.out(1034): /76.74.166.78:80(0)
11-21 15:50:03.330: I/System.out(1034): Socket[addr=/0.0.0.0,port=0,localport=33818]
11-21 15:50:03.331: I/System.out(1034): [socket][/192.168.1.6:33818]
11-21 15:50:03.341: I/System.out(1034): setSoSndTimeout:0
11-21 15:50:03.342: I/System.out(1034): setSendBufferSize:8096
11-21 15:50:04.164: I/System.out(1034): close [socket][/0.0.0.0:33818]
11-21 15:50:04.165: I/System.out(1034): close [socket][/0.0.0.0:33818]

Please help. Thank you.

Upvotes: 0

Views: 1133

Answers (1)

user207421
user207421

Reputation: 311050

When you ignore every possible exception, this kind of error is inevitable. A try/catch block in the middle of a method with more code after it is automatically suspect. Try catching, logging, and handling exceptions properly, by which I mean not continuing after they happen as though they hadn't happened.

Upvotes: 1

Related Questions