试图在封闭的流上阅读

时间:2012-11-21 09:16:45

标签: android sockets httpclient inputstream

当我滚动ListView并在到达列表中的最后一项时获取新数据时,我收到此异常。

这是获取新数据的代码:

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) {
        }
    }
}

我使用的客户端是:

private static DefaultHttpClient client = getThreadSafeClient();

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;
}

当我们滚动到ListView的底部时,方法 PostData 会获得新的10个项目。

问题是,这个工作正常,前3-4个卷轴,后来我得到了Exception,添加的新10个项目来自之前的数据(复制)。

有些时候我甚至得到这个例外:

Socket closed

取代

Attempted read on closed stream

但我没有使用任何套接字来尝试修复它。

修改

实际上我已登录每个catch块,它会打印Exception。但我没有在问题中添加所有这些日志。我在LogCat中获得的那些列在上面。

我一直在Logcats信息中获取这些行:

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]

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:1)

当您忽略每个可能的异常时,这种错误是不可避免的。方法中间的try/catch块,在自动怀疑之后会有更多代码。尝试正确地捕获,记录和处理异常,我的意思是在它们发生之后不再继续,就像它们没有发生一样。