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
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell
Accepted Solution

QNetworkReply running into issue loading JSON data

Hi,

 

I'm a newby with C++ and QT but so far I'm starting to like Cascade NDK !!!  

 

I'm trying to load a json data file which is fetch via a http request. Everything goes throught but my json data would just not load into the QVariantList. So after a few hours of poking arround, I finally noticed that the json data that is returned from the http request is missing two brakets [] (one @ start and one @ end).

When I load the json data from a file with the two brakets included, the QVariantList loads properly and I can debug throught the records...

 

Now my question is... from C++ how can I add those to [ ] brakets... See sample code below :

 

void MyJSONReadClass::httpFinished()
{
  JsonDataAccess jda;
  QVariantList myDataList;

  if (mReply->error() == QNetworkReply::NoError)
  {
    // Load the data using the reply QIODevice.
    qDebug() << mReply;
    myDataList = jda.load(mReply).value<QVariantList>();
  }
  else
  {
    // Handle error
  }

  if (jda.hasError())
  {
    bb::data&colon;:DataAccessError error = jda.error();
    qDebug() << "JSON loading error: " << error.errorType() << ": "
        << error.errorMessage();
    return;
  }


  loadData(myDataList);

  // The reply is not needed now so we call deleteLater() function since we are in a slot.
  mReply->deleteLater();
}

 

 

Also, I would have thought that the jda.hasError() would have capture this issue... but  guess not !

 

Am I using the wrong approach or wrong classes ??? The base example used is the WeatherGuesser project.

 

Thanks for you help...

Developer
Posts: 110
Registered: ‎10-22-2011
My Device: PlayBook, Bold 9650, Dev Alpha A
My Carrier: Sprint

Re: QNetworkReply running into issue loading JSON data

You might want to look into the sample code provided by RIM. In this case it seems the JSON Read/Write sample is a good example to learn from.

 

Most of its C++ work is done in app.cpp:

 

https://github.com/blackberry/Cascades-Samples/blob/master/jsonreadwrite/src/app.cpp

 

I personally suggest cloning Cascades-Samples into Momentics to look at sources rather than trying to use the GitHub views, btw. Smiley Happy



Is there a helpful or useful post in this thread? Click the thumbs up on it so that other people can find it more easily!
Like this post too! Smiley Very Happy
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: QNetworkReply running into issue loading JSON data

This might be unrelated to brackets. Try fetching the data from QNetworkResponse as QByteArray then load it into JsonDataAccess using loadFromBuffer function:

 

 myDataList = jda.loadFromBuffer(mReply.readAll()).value<QVariantList>();

 

If this won't help, you can add brackets this way (untested, please check the docs for proper function names if this won't compile):

 

QByteArray a = mReply.readAll();
a.insert(0, '[');
a.append(']');
myDataList = jda.loadFromBuffer(a).value<QVariantList>();

Note that if response data is zero terminated (most likely it isn't, but there's a possibility of this), you'll have to check if last symbol in byte array is '\0' and insert bracket before it.

 

Docs for QByteArray:

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


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: QNetworkReply running into issue loading JSON data

Thanks Zmey... I had to manually add the brakets... Now everything is working Smiley Happy