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
Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10
Accepted Solution

JsonDataAccess problem

[ Edited ]

Hi,

 

iam trying to load a json file that ive downloaded. (download works, checked if file exists in my "data/blabla.json")

But now when i'm trying to use JsonDataAccess it returns the following error while building

 

expected ';' before 'jda' OR 'JsonDataAccess' was not declared in this scope

 

 

the only thing i did:

 

#include <bb/data/JsonDataAccess.hpp>
/* ... */
void app::parseJson(){
    if (json->exists()) { JsonDataAccess jda;     }
}

 Does anybody have a idea?

 

Thx

 

// EDIT

 

after adding

using namespace bb::data;

it worked!

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: JsonDataAccess problem

i just wanted to add that you have to use the namespace, but you solved it yourself, even better :smileyhappy:
----------------------------------------------------------
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
Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10

Re: JsonDataAccess problem

yeah but what i think is strange is that i tried to do it like this bb:data:JsonDataAccess but this didn't work..

a well it is working now :smileywink:

Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: JsonDataAccess problem

probably because it would have to be bb::data::JsonDataAccess not bb:data:JsonDataAccess :smileyhappy:

 

I swear, the number of questions posted on these forums that are solved by namespace issues goes up every day

Like all of my posts
Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10

Re: JsonDataAccess problem

o lol :Whistling: yeah i know made a mistake in the quickreply.

Regular Contributor
mohdfarhanakram
Posts: 73
Registered: ‎12-30-2011
My Device: All devices

Re: JsonDataAccess problem

Hi Friend,

 

can you please post your source code ?

 

Because i want to take idea how you download json and write the file , please help me

 

 

Thanks

Mohd Farhan
Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10

Re: JsonDataAccess problem

[ Edited ]

Sure,

 

In your header file register the slots and the vars

 

 

private slots:
void requestFinished(QNetworkReply *reply);

private:
QNetworkAccessManager *mNetworkAccessManager;
QFile *file;

for example (pseudo code)

// App.h

#ifndef APP_H_
#define APP_H_

//* includes & namespaces here *//
class App{
public: App();
Q_INVOKABLE void init(); private slots: void requestFinished(QNetworkReply *reply); private: QNetworkAccessManager *mNetworkAccessManager; QFile *json; }; #endif /* APP_H_ */

 

Then in your constructor create the network access manager like this

mNetworkAccessManager = new QNetworkAccessManager(this);

 then add  the signal and slots and make sure to check them after that

bool result = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

Q_ASSERT(result);
Q_UNUSED(result);

 what i did was create the file refference here

 

json = new QFile("data/file.json");

 then call the init method to start the request, i think the Q_INVOKABLE is only needed when you call the init method from QML

init();

// init method looks like this
void App::init(){
QNetworkRequest request = QNetworkRequest();
    request.setUrl(QUrl("url to json"));
    mNetworkAccessManager->get(request);
}

and finaly in the SLOT we used requestFinished(QNetworkReply *reply) so this is how this method looks like

 

void App::requestFinished(QNetworkReply *reply){
	qDebug("Request finished");
	qDebug() << reply->error();
	qDebug() << reply->errorString();
	if (reply->error() == QNetworkReply::NoError) {
		qDebug() << "No error";

		QByteArray data = reply->readAll();

		if (!json->open(QIODevice::ReadWrite)) {
			qDebug() << "Failed to open file";
			return;
		}

		json->write(data);
		json->flush();
		json->close();

		qDebug() << "JSON file fetched and saved to 'data/file.json'";
}

 

Hope this helps.

 

Greets,

 

Xtravanta

Regular Contributor
mohdfarhanakram
Posts: 73
Registered: ‎12-30-2011
My Device: All devices

Re: JsonDataAccess problem

Thanks Xtravanta

Thanks

Mohd Farhan
Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10

Re: JsonDataAccess problem

no problem :smileyhappy: