Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:26:23+00:00 2026-06-14T20:26:23+00:00

My ListView gets data from a web server. Initially 10 items will be fetched

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:26:24+00:00Added an answer on June 14, 2026 at 8:26 pm

    Your method PostData() isn’t thread safe because you are using two objects not defined in the method:

    is = entity.getContent();
    sb = new StringBuilder();
    

    This results in the sockect from other running thread to be closed by:

    is.close();
    

    Define both of them locally to the method and it should solve the issue.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have facing problem in listview i am getting data from server and after
I have this class, that gets the data from the database and shows in
I am trying to build a webapp that gets data from server and shows
I have dynamic listview on my android client app that receive data from remote
I am showing enormous views on tabs formed with listview. ListView gets its data
I have a ListActivity which gets its data from a database query. I also
I get this exception when i scroll my ListView and fetch new data when
I have a sortable listview that gets filled with a live data as it
I have a ListView Activity that uses a AsyncTask to load data from the
Hy! I load data from my Db and I will show it in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.