My ListView gets data from a web server.
Initially 10 items will be fetched and later onScroll 10 more and so on.
This works fine for 4-5 scrolls but later gives this Exception:
java.net.SocketException: Socket closed.
after this:
E/OSNetworkSystem(27034): JNI EX in read
and sometimes instead of Socket closed, even this Exception occurs:
java.io.IOException: Attempted read on closed stream
and then instead of adding 10 new items, it adds 10 previously fetched items. This is because I’m not replacing the items in the ArrayList used in the Adapter, because of the Exception.
What causes this Exception?
EDIT:
It works fine for first 3-5 scrolls, then gives me some wrong data (previous data) because of this Exception and if I continue scrolling, it works fine later and again gives the Exception later, randomly.
Code to fetch list items:
postData to get data from the web server
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;
}
ThreadSafeClient i am using in PostData
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 in my Activity to get new Data onScroll. This is called in 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();
}
}
Its a long code, so pasting just the important lines
Any help appreciated.
EDIT:
After changing the PostData method to this, it worked fine.
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;
}
So, is it because is(InputStream) and sb(Stringbuilder) are not Local Variables in the previous case?
Thank You
Your method
PostData()isn’t thread safe because you are using two objects not defined in the method:This results in the sockect from other running thread to be closed by:
Define both of them locally to the method and it should solve the issue.
Regards.