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: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi
Accepted Solution

QNetworkReply is emptied after reading to jda

[ Edited ]

I'm trying to qDebug a QNetworkReply but only when an error has occured in my json parsing.

 

qDebug() << reply->readAll();

 I couldn't figure out why the reply was always returning an empty string. When I showed a friend he said because the reply has already been read the 'cursor' was at the end of the file and I needed to move the cursor back to the beggening.

 

EDIT: I'm now implimenting the seek function and it seems to executre successfully but reply is still returning a blank string. See post #3 for a simplified versoin of the function.

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi

Re: Seek 0 on QNetworkReply

[ Edited ]

...

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi

Re: Seek 0 on QNetworkReply

[ Edited ]

The full function which show's exactly what I'm trying to do is as follows.

 

void CascadesRest::requestFinished (QNetworkReply *reply) {
QVariant obj = jda->load(reply);

if (reply->error() == QNetworkReply::NoError && !jda->hasError()) {
  // some code
} else if (jda->hasError()) {
  if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString().indexOf("204") != -1) {
    // some code
  } else {
// This is where I'm trying to get the raw QNetworkReply and qDebug it
// Seek seems to work successfully but I'm still getting back "" for reply
// I checked position using reply->pos() here and it's 0
reply->seek(0); qDebug() << reply->pos(); } }
reply->deleteLater(); }

 

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Seek 0 on QNetworkReply

QNetworkReply is a sequential stream and doesn't support seek.

 

This function can be rewritten like this (untested):

 

void CascadesRest::requestFinished (QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{ QByteArray data = reply->readAll(); QVariant obj = jda->loadFromBuffer(data);
// Use 'data' for other tasks
}
reply->deleteLater(); }

Also, I suggest modifying the function to check reply->error() first, and only on success to proceed with reading/parsing of the data.

 

There are also predefined constants for most HTTP errors. This line:

if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString().indexOf("204")

Can be rewritten as:

if (reply->error() == QNetworkReply::AuthenticationRequiredError)

Other codes can be found on this page:

http://qt-project.org/doc/qt-4.8/qnetworkreply.html

 


Andrey Fidrya, @zmeyc on twitter