Author Topic: Http Connection  (Read 9290 times)

yhat

  • Newbie
  • *
  • Posts: 5
    • View Profile
Http Connection
« on: June 09, 2006, 06:45:42 am »
Hi! Currently I'm developing an application which will deal with the http connection. For the http connection, I will send out my request to download an image. The problem I'm facing right now is when there's no response back for my request, my whole application is hang. I totally unable to exit the application and need to reset the device all the time. Here, I would like to ask whether is there anyone can help me to handle the no response back for the http connection. I'm using the below method for my http connection.

HttpConnection connection = null;
connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
DataInputStream iStrm = connection.openDataInputStream();
ByteArrayOutputStream mSrm = null;
int length = (int) connection.getLength();
mData = new byte[length];          mSrm.readFully(mData);

After opening the connector, I need to close the connector when receiving the response back from my request. If there's no response back, my connector is always open and this cause my application hang.

Is there way to close the connector if there's no response back from the request made to http connection?

Please help....


YHat

Omicron

  • Hero Member
  • *****
  • Posts: 608
    • View Profile
    • http://
Http Connection
« Reply #1 on: June 09, 2006, 02:19:07 pm »
Quote
Hi! Currently I'm developing an application which will deal with the http connection. For the http connection, I will send out my request to download an image. The problem I'm facing right now is when there's no response back for my request, my whole application is hang. I totally unable to exit the application and need to reset the device all the time. Here, I would like to ask whether is there anyone can help me to handle the no response back for the http connection. I'm using the below method for my http connection.

HttpConnection connection = null;
connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
DataInputStream iStrm = connection.openDataInputStream();
ByteArrayOutputStream mSrm = null;
int length = (int) connection.getLength();
mData = new byte[length];    mSrm.readFully(mData);

After opening the connector, I need to close the connector when receiving the response back from my request. If there's no response back, my connector is always open and this cause my application hang.

Is there way to close the connector if there's no response back from the request made to http connection?

Please help....


YHat
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]


First of all. I am quoting Java 1.5, so some of this may or may not be applicable.

However, the docs for 1.5 are here:
[a href=\"http://java.sun.com/j2se/1.5.0/docs/api/]http://java.sun.com/j2se/1.5.0/docs/api/[/url]

I ussually use HttpURLConnection and not HttpConnection, so I am not sure of the differences since 1.5 has no HttpConnection class per se.

ANyway, I think the problem is in the URL class anyway, but upon searching the URL class I came up with nothing, however that lead me to loo at the URLConnection class (sorry, not sure how URLCOnnection Class and URL class work together) and I found this:

URLConnection.setConnectTimeout(int timeout)
          Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.

My guess is that there is already a timeout on your connection but it is probably 2 or more minutes long, which of course is too much for you needs.

Anyway, this should give you a start, but you may need to upgrade to a FULL 1.5 implementation to use these classes (though there may very well be a similar funcatioanilty is the J2ME libs).

Regards.
"You Shall Not Pass"    
....Gandalf, Lord Of The Rings
--------------------------------------------------------------
C-860 (Cacko), 3x4gb MD  
DLINK 660W, 1GB SD,  
Upgraded Archos AV320 w/80GB HDD
Pocketop and Targus IR keyboards
Favorite Deal Site: SaveCity.net (pretty cool, good deals daily on one page)

Meanie

  • Hero Member
  • *****
  • Posts: 2803
    • View Profile
    • http://www.users.on.net/~hluc/myZaurus/
Http Connection
« Reply #2 on: June 10, 2006, 09:20:10 am »
what you need to do is create a new thread and call http connection in the new thread. this way you can have multiple concurrent downloads happening as well as controls to terminate or interrupt each of those threads individually. you might also want to tie a progressbar to each of those threads for a visual effect showing the progress of the downloads, but you will probably need to implement swing worker pattern which may not be available to you depending on the version of the jvm you are running, ie has to be > 1.2.x
SL-C3000 - pdaXii13 build5.4.9 (based on pdaXrom beta3) / SL-C3100 - Sharp ROM 1.02 JP (heavily customised)
Netgear MA701 CF, SanDisk ConnectPlus CF, Socket Bluetooth CF, 4GB Kingston CF,  4GB pqi SD, 4GB ChoiceOnly SD, 2GB SanDisk SD USB Plus, 1GB SanDisk USB Plus, 1GB Transcend SD, 2GB SanDisk MicroSD with SD adaptor, Piel Frama Leather Case, GoldX 5-in-1 USB cable, USB hub, USB mouse, USB keyboard, USB ethernet, USB HDD, many other USB accessories...
(Zaurus SL-C3000 owner since March 14. 2005, Zaurus SL-C3100 owner since September 21. 2005)
http://members.iinet.net.au/~wyso/myZaurus - zBook3K