Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 115
Registered: ‎11-16-2011
My Device: Smartphone Storm 9900
My Carrier: 3

How to set time out request connection?

Hy everybody,,,,

i have a connection with http connection on thread...

i want to set specified time to time out, for example i want to force thread and show dialog timeOut information if connection more than 10000ms... so user not wait too long... any body help me?

 

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to set time out request connection?

you can use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/SocketConnectionEnhanced.ht... for direct tcp connections, or ";ConnectionTimeout=30000;deviceside=false" for a MDS connection.
As far as i know other connection types do not support a timeout.

the new connectionfactory encapsulates these into http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/transport/ConnectionFactory...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to set time out request connection?

If you use ConnectionFactory, you can set a connection timeout on that.  I have not actually attempted this nor do I know what happens when the time is exceeded, sorry,

 

Another option is to start your own Timer, and then try to cancel the connection if the Timer returns before the connection is complete.  In this circumstance you can allow the stalling Dialog to continue so the user does not wait and ignore the results of the request if it does eventually come back.  Not exactly stopping it but the effect from a user perspective is the same. 

Trusted Contributor
Posts: 115
Registered: ‎11-16-2011
My Device: Smartphone Storm 9900
My Carrier: 3

Re: How to set time out request connection?

my part connection like this,

connection = (HttpConnection) Connector.open("http://ecc.ft.ugm.ac.id/?r=blackberryapp/vacancybycomp&offset="+offset+connParameters);
		    connection.setRequestMethod(HttpConnection.GET);
		    connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_TYPE,"application /x-www-form-urlencoded");
		    os = connection.openOutputStream();
		
		      if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
		      is=connection.openInputStream();
		    
		              String xml = "";
		              int ch;

		              while((ch=is.read()) != -1){
		            	  sb.append((char)ch);

		              }
		              xml = sb.toString();

 

any the best way to set time out on httpconnection...

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to set time out request connection?

use ConnectionFactory
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 14
Registered: ‎01-31-2011
My Device: Simulator
My Carrier: Developer

Re: How to set time out request connection?

ConnectionFactory doesnt support  setting timeout(BlackBerry API 5.0.0 ) tough it has a default timeout. Custom made timer can be a good idea as peter said. 

----------------------------------------------------------
Please press the like button on the right side to thank the person who helped you.
Please mark the post as solved if you found it as solution.
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to set time out request connection?

setConnectionTimeout
public void setConnectionTimeout(long connectionTimeout)
Sets connectionTimeout to the desired value

Parameters:
connectionTimeout - the connectionTimeout to set (in milliseconds)
Since:
BlackBerry API 5.0.0
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Trusted Contributor
Posts: 115
Registered: ‎11-16-2011
My Device: Smartphone Storm 9900
My Carrier: 3

Re: How to set time out request connection?

Ok im have try connection whit connectionFactory... like this

	MyConnectionFactory connect = new MyConnectionFactory();
        			connect.setConnectionTimeout(2000);
        			connect.setTimeLimit(2000);
        			ConnectionDescriptor conDesc = null;
        			try{
        			    conDesc = connect.getConnection("http://192.168.1.26/eccx/?r=blackberryapp/moreindex&id_comp="+x+"&offset="+offset);
        			}catch(Exception e){
        			    System.out.println(e.toString()+":"+e.getMessage());
        			}
        			//String response = "";
        			if(conDesc!=null){
        				try{
        			       connection = (HttpConnection)conDesc.getConnection();
        			       connection.setRequestMethod(HttpConnection.GET);
        			       connection.setRequestProperty("Connection", "close"); // close the connection after success sending request and receiving response from the server
       			           connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       			           os = connection.openOutputStream();
       	        		
              		  //  os.write(postBytes);
       			           os.flush();
       			           os.close();

       			           if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
       			        	   is=connection.openInputStream();
              		    
              		              String xml = "";
              		              int ch;

              		              while((ch=is.read()) != -1){
              		            	  sb.append((char)ch);

              		              }
              		              xml = sb.toString();
              		
              					jso = new JSONObject(xml); 
       			           }
        			        
        				}catch(Exception e){
        			      //  System.out.println(e.toString()+":"+e.getMessage());
        			        Dialog.alert("error, time-out");
        			    }

 code above is to try if the connection timeout, how to catch the error? and show dialog alert, where i must put the code?