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

Native Development

Reply
Highlighted
Developer
Posts: 116
Registered: ‎03-15-2011
My Device: Blackberry 9800
My Carrier: developer
Accepted Solution

How to configure connection timeout in blackberry10

Hi All,

 

I am using below code for network request.

 

QNetworkRequest request = QNetworkRequest();
request.setUrl(QUrl("http://www.example.com"));
_networkAccessManager->get(request);

As per my observation, default connection timeout for network reaquest is 60 secs. 

How to configure this timeout to 20 secs.

 

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

Re: How to configure connection timeout in blackberry10

i wrote my own timeout class, QNetworkTimeout.
It uses qTimer to abort the network reply (which i use as a parent).
I connected downloadProgress, uploadProgress, metaDataChanged and authenticationRequired to avoid killing an active 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
Developer
Posts: 116
Registered: ‎03-15-2011
My Device: Blackberry 9800
My Carrier: developer

Re: How to configure connection timeout in blackberry10

thanks Simon_hain for your prompt response. Please provide source code of QNetworkTimeout class.
New Developer
Posts: 10
Registered: ‎08-23-2013
My Device: Blackberry Z10
My Carrier: Bell Mobility

Re: How to configure connection timeout in blackberry10

I was just going through the same issue until I found the below code to add.

The only way I found is to add a QTimer around it which will call QNetworkReply::abort

 

QNetworkReply* response = m_networkAccessManager->get(request);
replyPWPointer = response; QTimer* timer = new QTimer(this); timer->setSingleShot(true); bool ok2 = connect(timer,SIGNAL(timeout()),this,SLOT(replyPWPointer())); timer->start(3000);

replyPWPointer is directly in my class.

 

Then my Slot

 

replyPWPointer->abort();

 

I am not entirely satisfied because this works only if you send one request at a time, but if two were to be sent before the previous one gets aborted or finished, it will crash since it will try to abort the same one twice.

Let me know if you come up with something better.

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to configure connection timeout in blackberry10

toolonglyf, it should work with any number of connections. It crashes because replyPWPointer variable is overwritten. I suggest modifying it like this (untested, but should work):

 

QNetworkReply* response = m_networkAccessManager->get(request);
QTimer* timer = new QTimer(response); // !!!! set response as parent
timer->setSingleShot(true);
bool ok2 = connect(timer,SIGNAL(timeout()),this,SLOT(replyPWPointer()));
timer->start(3000);

// Now in replyPWPointer slot use parent() to get QNetworkReply instance:

void ClassName::replyPWPointer() {
  QTimer *timer = qobject_cast<QTimer *>(sender());
  QNetworkReply *response = qobject_cast<QNetworkReply *>(timer->parent());
  response->abort();
}

// Timer will be destroyed when the reply is destroyed because the reply is it's parent.

 

 


Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 10
Registered: ‎08-23-2013
My Device: Blackberry Z10
My Carrier: Bell Mobility

Re: How to configure connection timeout in blackberry10

This works perfectly, thanks !