当我滚动我的 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 卷,这很好用,后来我得到了异常,新添加的 10 个项目来自以前的数据(重复)。
有时我什至得到这个例外:
Socket closed
代替
Attempted read on closed stream
但我没有使用任何Sockets来尝试修复它。
编辑:
实际上,我在每个 catch 块中都有登录,它会打印异常。但我没有在问题中添加所有这些日志。上面列出了我在 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]
请帮忙。谢谢你。