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
Regular Contributor
dhirajnangare
Posts: 93
Registered: ‎08-20-2013
My Device: BB 10 -BlackBerry Z10

Working with server

I am developing one application in which I need to interact with server. I want to post some data to server . Can anybody tell me how should I do this ? or provide me link  so that i could get some info about it

Regular Contributor
Q-T
Posts: 94
Registered: ‎12-02-2011
My Device: non-Blackberry user

Re: Working with server

QTcpSocket is one way.

Can also look at QNetworkRequest

Regular Contributor
dhirajnangare
Posts: 93
Registered: ‎08-20-2013
My Device: BB 10 -BlackBerry Z10

Re: Working with server

I am new to this technology so I didnt get what you said . do you know any sample app for this 

Developer
maarko
Posts: 119
Registered: ‎07-02-2013
My Device: Q10 v10.2.0.424

Re: Working with server

here you go...

 

some doc to the basic class:

https://developer.blackberry.com/native/reference/cascades/qnetworkrequest.html

 

some example - way too big, but you can extract just what you need:

https://developer.blackberry.com/native/documentation/cascades/device_comm/networking/developing_the...

 

the essentials of all above:

 

 

void NetworkRequest::NetworkRequest(){
	QNetworkAccessManager *netaccess=new QNetworkAccessManager(this); //one per entire app

	bool cr;
	cr=connect(	netaccess, SIGNAL(finished(QNetworkReply*)),
				this, SLOT(onReplyFinished(QNetworkReply*))
	);
	Q_ASSERT(cr);
}

void NetworkRequest::getMethod(const QString &url){
	netaccess->get(QNetworkRequest(QUrl(url)));
}

void NetworkRequest::onReplyFinished(QNetworkReply* reply){
	if(reply->error()==QNetworkReply::NoError){
		emit replyReceived(reply->url().toString(), reply->readAll());
	}
	else{
		emit errorReceived(reply->url().toString(), reply->errorString());
	}
	reply->deleteLater();
}

 

if you do this right way, you call getMethod with URL and you get signal replyReceived() with all data read if everything went ok, or errorReceived() signal with error message.

Developer
maarko
Posts: 119
Registered: ‎07-02-2013
My Device: Q10 v10.2.0.424

Re: Working with server

the example is for GET but obviously you can manage it for POST

Regular Contributor
dhirajnangare
Posts: 93
Registered: ‎08-20-2013
My Device: BB 10 -BlackBerry Z10

Re: Working with server

[ Edited ]

I tried to run the given example which in on  this link  

 

https://developer.blackberry.com/native/documentation/cascades/device_comm/networking/developing_the...

 

but giving me error  in .hpp file of StatusEvent class at  

netstatus_info_t* info;

 

'netstatus_info_t' does not name a type. Is this because of SDK version issue . I am using 10.0.9 SDK version.

 

Developer
Benecore
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: Working with server

[ Edited ]

StatusEvent class is specially for monitoring changes of network connections

 

QNetworkAccessManager is what you want for start

GET example

void doGET(){

    QNetworkAccessManager connection;
    
    QUrl url("http://blabla");
    url.addQueryItem("parameter", "14"); // if you need parameters

    QNetworkRequest req(url);
    
    QNetworkReply *reply = connection.get(req);
    connect(reply, SIGNAL(finished()), SLOT(getFinished()));
}

void getFinished(){ QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); if (reply->error() == QNetworkReply::NoError){ // No error QString result = reply.readAll(); } else{ // error occured int errorCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << errorCode << endl << reply->errorString(); } reply->deleteLater(); }

POST example

void doPOST(){

    QNetworkAccessManager connection;
    
    QUrl url("http://blabla");
    url.addQueryItem("parameter", "14"); // if you need parameters

    QNetworkRequest req(url);

    QByteArray postParameters;
    postParameters += "paremater1=";
    postParameters += "value1";
    postParameters += "&";
    postParameters += "paremater2=";
    postParameters += "value2";
    
    QNetworkReply *reply = connection.post(req, postParamaters);
    connect(reply, SIGNAL(finished()), SLOT(postFinished()));
}


void postFinished(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if (reply->error() == QNetworkReply::NoError){
        // No error
        QString result = reply.readAll();
    }
    else{
        // error occured
        int errorCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        qDebug() << errorCode << endl << reply->errorString();
    }
    reply->deleteLater();
}

This is one of many others workaround how to make GET/POST



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Regular Contributor
dhirajnangare
Posts: 93
Registered: ‎08-20-2013
My Device: BB 10 -BlackBerry Z10

Re: Working with server

QByteArray postParameters;
    postParameters += "paremater1=";
    postParameters += "value1";
    postParameters += "&";
    postParameters += "paremater2=";
    postParameters += "value2";

 what is this part in above code ? Is this mean, the data which we want to post 

Developer
Benecore
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: Working with server

[ Edited ]

Yes

:smileywink:

connection.post(req, postParamaters);

You can post also empty paramaters

connection.post(req, QByteArray());

little easier code to generate post parameters

QUrl postParamaters;
postParamaters.addQueryItem("param1", "value1");
postParamaters.addQueryItem("param2", "value2");
postParamaters.addQueryItem("param3", "value3");
postParamaters.addQueryItem("param4", "value4");

connection.post(req, postParamaters.encodedQuery());

 Check this

https://developer.blackberry.com/native/reference/cascades/qnetworkaccessmanager.html#post

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Regular Contributor
dhirajnangare
Posts: 93
Registered: ‎08-20-2013
My Device: BB 10 -BlackBerry Z10

Re: Working with server

I used this code in my application with my api url and in postFinished() method I have shown result in toast  I am getting  "invalid access" in toast as a reply.  So why is this happening ? :Helpsmilie: