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
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

timeout in URL Connection

Hello Every Body, i am working on application which send and recieves XML responses.. I just want to make my application in such away that, if i didnot get any XML response, then it should timed out.. I have pasted my code below..Kindly help me out there. My Timeed out is not working properly.

 

ConnectionFactory connect = new ConnectionFactory();
connect.setConnectionTimeout(2000);
connect.setTimeLimit(2000);
HttpConnection connection = null;//(HttpConnection) Connector.open(url);
ConnectionDescriptor conDesc = null;
try{
conDesc = connect.getConnection(url);
}catch(Exception e){
xmlRespone = new String[1][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = e.getMessage();
return xmlRespone;
}
connection = (HttpConnection) conDesc.getConnection();
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_TYPE,"application /x-www-form-urlencoded");


DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(connection.openInputStream());
if (connection.getResponseCode() != javax.microedition.io.HttpConnection.HTTP_OK){
xmlRespone = new String[2][2];
xmlRespone[0][0] = String.valueOf(connection.getResponseCode());
xmlRespone[0][1] = String.valueOf(connection.getResponseCode());
xmlRespone[1][0] = String.valueOf(connection.getResponseCode());
xmlRespone[1][1] = String.valueOf(connection.getResponseCode());
return xmlRespone;
}

doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
_node=new String();
_element = new String();
xmlRespone = new String[list.getLength()][2];


for (int i=0;i<list.getLength();i++)
{
Node value=list.item(i).
getChildNodes().item(0);

_node=list.item(i).getNodeName();
if(value==null)
{_element=null;}
else
{_element=value.getNodeValue();}
xmlRespone[i][0] = _node;
xmlRespone[i][1] = _element;

}//end for

//end try
//will catch any exception thrown by the XML parser
}
catch (IOException ioe)
{
xmlRespone = new String[2][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = ioe.getMessage();
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "13";
return xmlRespone;
}
catch (Exception e)
{
xmlRespone = new String[2][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = e.getMessage();
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "14";
return xmlRespone;
}

return xmlRespone;
}

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: timeout in URL Connection

"My Timeed out is not working properly."

 

What does this mean?  It works sometimes and doesn't work other times?  Or it is woprking but not giving you the correct results?  Or is it working but not giving you the correct time?

 

In addition, can I suggest before you post, that you search to make sure the problem is new:

http://supportforums.blackberry.com/t5/Java-Development/Consistent-HTTP-Timeout-setting/m-p/1973759

Please review this Thread and make sure you understand it before looking any further at this problem. 

 

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: timeout in URL Connection

2 seconds is also very very short for a timeout on a mobile data connection.
----------------------------------------------------------
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
Please use plain text.
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: timeout in URL Connection

Dear Peter,

                     It is not working..I mean Timeout

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: timeout in URL Connection

Have you reviewed the Thread I pointed you at.

Please use plain text.
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: timeout in URL Connection

Dear Peter,

I have reviewed your Thread. I made changings in my Coding... TimerTask works fine for the firt time, but it not updated its self. Forexample if its timer is of 10 seconds, then after getting any XML Repsonse it will not update its self, and what ever the transaction i will made... Kindly help me out there. I am sending my Code below:

 

 

 

public static String[ ][ ] AgAppXMLParserLoginRegis(String parUrl) {

Document doc;
StreamConnection conn;
String _node,_element;
String[ ][ ]  xmlRespone = null;
try{

String url = AgAppHelperMethods.getUrl() + parUrl;
url = AgAppHelperMethods.getNetworkDependentSuffixedURL(url);

if(url == null)
{
xmlRespone = new String[1][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = "No Connection Available!";
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "12";
return xmlRespone;
}

timer = new Timer();
timer.schedule(new TimerTask() {

public void run() {
// TODO Auto-generated method stub
try {
if(connection!=null){
connection.close();
timer.cancel();
timer = null;
}

} catch (IOException e) {
e.printStackTrace();
}
}
}, 1, 95000); 


ConnectionFactory connect = new ConnectionFactory();

connect.setTimeoutSupported(true);
connect.setConnectionTimeout(100);
connect.setTimeLimit(100);


ConnectionDescriptor conDesc = null;
try{
conDesc = connect.getConnection(url);
}catch(Exception e){
xmlRespone = new String[1][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = e.getMessage();
return xmlRespone;
}

connection = (HttpConnection) conDesc.getConnection();
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_TYPE,"application /x-www-form-urlencoded");


DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(connection.openInputStream());
if (connection.getResponseCode() != javax.microedition.io.HttpConnection.HTTP_OK){
xmlRespone = new String[2][2];
xmlRespone[0][0] = String.valueOf(connection.getResponseCode());
xmlRespone[0][1] = String.valueOf(connection.getResponseCode());
xmlRespone[1][0] = String.valueOf(connection.getResponseCode());
xmlRespone[1][1] = String.valueOf(connection.getResponseCode());
return xmlRespone;
}

timer.cancel();
timer = null;

doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
_node=new String();
_element = new String();
xmlRespone = new String[list.getLength()][2];


for (int i=0;i<list.getLength();i++)
{
Node value=list.item(i).
getChildNodes().item(0);

_node=list.item(i).getNodeName();
if(value==null)
{_element=null;}
else
{_element=value.getNodeValue();}
xmlRespone[i][0] = _node;
xmlRespone[i][1] = _element;

}//end for

//end try
//will catch any exception thrown by the XML parser
}
catch (IOException ioe)
{
xmlRespone = new String[2][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = ioe.getMessage();
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "13";
return xmlRespone;
}
catch (Exception e)
{
xmlRespone = new String[2][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = e.getMessage();
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "14";
return xmlRespone;
}

//AgAppProfile ap = new AgAppProfile();
//ap.writeLog("Receiving");
return xmlRespone;
}

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: timeout in URL Connection

I think you need to review the API.  What does this do?

timer.schedule(..., 1, 95000);

What do you want it to do?

 

Compare this line in your code with the equivalent line in the code supplied in the Thread I pointed you at.

 

And from your code above, I actually don't understand these comments:

1) "TimerTask works fine for the firt time" are you sure - I honestly can't see how, with the values above?

2) "timer is of 10 seconds" - where do you set this time period?

3) "it will not update its self" - what is being updated?  Is 'it' the Timer, the TimerTask, the HttpConnection, the returned data or what?

 

Don't worry about these for now, first get how the Timer works clear in your own mind.  Then debug your and see what happens too.  For example, try a URL that will work quickly and a URL that will time out. 

 

If you can't get your code working using the hint above, then please come back but with more detai, clarifying the areas I am confused about please. 

 

Please use plain text.
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: timeout in URL Connection

Dear Peter Sir,

                         My English is not fine,thats why i am trying to explain u what i understand in my code.

 

Question Number (1): timer.schedule(..., 1, 95000);

  I used:

 

public void run() {


try {
if(connection!=null){         // If Connection Doesnot established then it will close the connection and stop the timer
connection.close();
timer.cancel();
timer = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}

 

timer.schedule(..., 1, 95000);

 

I used 1 for the delay, means to say it should update it self in regular intervals and 95000(95 Seconds) is the period in which it should check the status..

 

Question Number (2): timer is of 10 seconds" - where do you set this time period?

 

My timer is of 95 seconds not 10 seconds.  timer.schedule(..., 1, 95000);

 

 

Question Number (3): "it will not update its self" - what is being updated?  Is 'it' the Timer, the TimerTask, the HttpConnection, the returned data or what?

 

My timer is not updating. My first transaction for HttpConnection is LOGIN page which uses AgAppXMLParserLoginRegis(String parUrl) method , When i logged in an application and do any other transaction for Http Connection for example recieving any XML Response by pressing SUBMIT BUTTON which also uses AgAppXMLParserLoginRegis(String parUrl) then the timer doesnot work again. Timer should not be reset and what ever the transaction i should made in an application it is only applicable for 95 seconds from LOGIN Page, and when ever 95 seconds exceeds, then it stopped working...

 

 

 

 

 

 

 

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: timeout in URL Connection

I appreciate that English is not your first language, I was not being critical, just pointing out that I did not understand.

 

If you look at the Thread I pointed you at, you will see that the code in this Thread uses this method:

 

connectionTimerConnect.schedule(..., myConnectionTimeoutSeconds); // Kill the request after x seconds

 

This is a one off request - it will either be run if there is a delay longer than myConnectionTimeoutSeconds or be cancelled if the network time is shorter. 

 

The method you are using is intended to provide a recurring invocation of the TimerTask.  I do not understand why you would need a recurring invocation of the TimerTask   Especially as the first time it is run, it cancels the timer anyway.  It is this logic that I think you need to review. 

 

So my question was not asking what your TimerTask does.  It was asking why you were scheduling it to run more than once.  Remember that you establish a new connection each time you communicate with the Server.  The rational behind this logic is that you do not want any connection to take too long.  So you start a new Timer each time you start a new Connection. 

 

Now if you look at the API, the method you use is:

schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay

You have specified:

timer.schedule(..., 1, 95000);

So this will run your TimerTask after a delay of 1 millisecond, and then again 95 seconds later.  Is that what you wanted?

 

It appears you might have a different requirement to what I understood.  Please can explain your requirement again, becuase I did not understand this:

"Timer should not be reset and what ever the transaction i should made in an application it is only applicable for 95 seconds from LOGIN Page, and when ever 95 seconds exceeds, then it stopped working..."

 

From my reading, this could mean two different things:

 

1) "Once the login has completed, the user has 95 seconds to do whatever they need to do". 

If this is the case, then the logic above is not applicable.  The idea behind the Timer code here is to prevent a single request taking too long.  This logic does not belong in the connection code. 

 

2) "Once the login has been completed, the user must keep making requests frequently.  The application should force the user to login again, if a request is not made within 95 seconds."

Again this is a different requirement to the above, and you would not actually implement this in the connection code - you would do the checking in your application and reset the Timer each time you called AgAppXMLParserLoginRegis(String parUrl). 

Please use plain text.
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: timeout in URL Connection

Dear Peter Sir,
I really appreciate your quick response. This is the scenario which i want : "Remember that you establish a new connection each time you communicate with the Server. The rational behind this logic is that you do not want any connection to take too long."

Means to say on any transaction when ever i called AgAppXMLParserLoginRegis(String parUrl), TIMER should be reset to 95 seconds for that particular Transaction for Connection established time,if connection should not build up within 95 seconds then it should Cloase the connection.

Like:


Login Button : 95 seconds for this transaction.
2nd Request Button: 95 seconds for this transaction.
3rd Request Button: 95 seconds for this transaction.

 

But the problem is that it only works for login button only, and it not refresh for other transactions means timer should be of 95 seconds for other transactions as well.

 

If on 2nd Request Button connection is not established and Timer connection.close() established, then on 3rd Reuest Button Timer and Connection should work in the same manner that it work on LOGIN

Please use plain text.