URL and socket closing

by michael » Wed, 26 Mar 2008 10:12:15 GMT


Sponsored Links
 Hi,

I have a problem about correctly closing the underlying socket after
opening and reading an input stream from a URL object. The following
code leaves many (i.e. all) sockets open (in the CLOSE_WAIT state, as
netstat reports):

        private void test() {
                for (int i = 0; i < 10000; i++) {
                        try {
                                String urlStr = " http://test-url.com/?q=" ; + i;
                                URL url = new URL(urlStr);
                                InputStream is = url.openStream();
                                byte[] buffer = new byte[1024];
                                int len = is.read(buffer);
                                String content = "";
                                while (len != -1) {
                                        content += new String(buffer, 0, len);
                                        len = is.read(buffer);
                                }
                                is.close();
                        } catch (Exception e) {
                                Log.w(TAG,e);
                        }
                        Log.i(TAG, "i: " + i);
                }
        }

As a consequence, no more sockets can be opened after a while. The
problem does not seem to occur when running the same code in a
"normal" java program (i.e. not in android). Am I missing something
here? Or is this problem due to a bug in android? How could I read
many URLs without running into this problem? (I want to query a web
service with lots of different parameters, that's why I need to open
that many connections.)

By the way: The problem only occurs, if the urlStr changes in each
loop.

Thanks for any hints,

Michael


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Updating Views in a ListView when they may have been recycled

Hi,

I've recently got into Android development and am writing a basic
podcast app as a learning exercise. I've come up with an issue I can't
find a good solution for. Rather than post code (it's getting big!),
I'll try and summarise:

I have an ExpandableListView showing podcasts and each podcast's
episodes in a simple two-level tree. The podcasts are downloaded in a
Service, and this Service 'sends' messages by using an interface
registered by the main Activity. This interface then uses a handler to
update the UI.

The child views in the ExpandableListView are LinearLayouts with one
TextView and two ViewStubs, one for showing an image and one for
showing a horizontal ProgressBar. These layouts are created if needed
in my extended BaseExpandableListAdapter's getChildView, or reused by
using the convertView supplied.

This all works fine and I'm happy with the overall model, except that
I can't figure out how to update a download's ProgressBar efficiently
when the Activity receives a callback from the Service. How do I get a
reference to the correct View to set its progress?

I've tried keeping a cached HashMap to track which episode currently
has which View instance, but this didn't work due to me recycling the
Views via convertView - I found that when more than one download is
active the two bars can swap values back and forth.

Any help would be appreciated.

Thanks,

Neil

--~--~---------~--~----~------------~-------~--~----~

2. How to get the Count for the Outgoing SMS in between the particular date

Hi,

in Android Mobile i need to get the SMS count for the outgoing message
between the particular date /  or taken the count from the given date
to the current Date.


as the same like the call counts also out going call counts

Please Help me to found that  email me to my Id :
damu...@gmail.com


Thank you


--~--~---------~--~----~------------~-------~--~----~

3. develop picasa in android

4. French Version Of Datepicker

5. WifiReceiver not part of SDK 1.5???

6. how to package several android projects

7. R.arawable visuals