我的 ListView 从 Web 服务器获取数据。
最初将获取 10 个项目,然后再滚动 10 个,依此类推。
这适用于 4-5 卷轴,但后来给出了这个例外:
java.net.SocketException: Socket closed.
在这之后:
E/OSNetworkSystem(27034): JNI EX in read
有时而不是Socket closed,甚至会发生这种异常:
java.io.IOException: Attempted read on closed stream
然后不是添加 10 个新项目,而是添加 10 个以前获取的项目。这是因为我没有替换适配器中使用的 ArrayList 中的项目,因为异常。
是什么导致了这个异常?
编辑:
它适用于前 3-5 次滚动,然后由于此异常而给我一些错误的数据(以前的数据),如果我继续滚动,它稍后会正常工作,稍后会随机再次给出异常。
获取列表项的代码:
postData 从网络服务器获取数据
public static String PostData(String url, String sa[][]) {
DefaultHttpClient client = getThreadSafeClient();
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]));
Log.d(sa[i][0], "" + sa[i][1]);
}
HttpPost httppost;
try {
httppost = new HttpPost(baseUrl + url);
} catch (Exception e) {
}
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
}
HttpResponse response = null;
try {
response = client.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (Exception e) {
}
HttpEntity entity = response.getEntity();
try {
is = entity.getContent();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
try {
BufferedReader 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);
}
is.close();
result = sb.toString();
} catch (Exception e) {
}
return result;
}
我在 PostData 中使用的 ThreadSafeClient
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;
}
AsyncTask 在我的 Activity 中获取新的数据 onScroll。这在 onScroll 中被调用
AsyncTask{
ArrayList<item> itemList = new Arraylist<item>();
doInBackground(){
String response = PostData(url, sa);
//sa has namevaluepairs as a 2-dimensional array
JSONArray JArray = new JSONArray(response);
for(int i = 0; i < jArray.length; i++){
itemList.add(convertToItem(JArrya.getJSONObject("items")));
}
}
onPostExecute(){
for(int i = 0; i < itemList.size(); i++){
mListAdapter.add(itemList.get(i));
}
mListAdapter.notifyDataSetChanged();
}
}
它的代码很长,所以只粘贴重要的行
任何帮助表示赞赏。
编辑:
将PostData方法更改为此后,它工作正常。
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;
httppost = new HttpPost(baseUrl + url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = null;
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
String res = EntityUtils.toString(entity);
return res;
}
那么,是不是因为前一种情况下is(InputStream)和sb(Stringbuilder)不是局部变量呢?
谢谢你