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
msalamali
Posts: 39
Registered: ‎09-09-2013
My Device: Dev Alpha C
Accepted Solution

Weather sample code using QNetworkRequest

[ Edited ]

I'm trying to get Weather temperature using QNetworkRequest as follow:

 

===================================

QNetworkAccessManager qNam;
QNetworkReply *temperReply;

 

void Class::getWeatherTemperature(QString lat, QString lon){


temperReply = qNam.get(QNetworkRequest(QUrl("api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon)));

 

connect(temperReply, SIGNAL(finished()), this, SLOT(onFinishedTemperature()));

 

}

 

void Class:: onFinishedTemperature(){

QNetworkReply *weatherReply = dynamic_cast<QNetworkReply*>(sender());

qDebug()  << weatherReply->errorString() << weatherReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

 

//other code

}

 

==================================

 

but it does not work .. i got errorString:  Protocol "" is unknown 

 

 

 

is there weather sample code using QNetworkRequest? 

 

Be like Dan Brown, get the dimensions of every place you visit!
MeasureMe on App World
Developer
nemory
Posts: 217
Registered: ‎05-21-2012
My Device: BlackBerry RED Z10

Re: Weather sample code using QNetworkRequest

try check if it connected the signal and slot 

 

bool connected = connect(temperReply, SIGNAL(finished()), this, SLOT(onFinishedTemperature()));

 

qDebug() << "CONNECTED: " << connected;

Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

Re: Weather sample code using QNetworkRequest

Your URL needs to include the server & domain information, and also the protocol prefix:

 

QUrl qurl("http://server.domain.com/api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon);

 

temperReply = qNam.get(QNetworkRequest(qurl));

 

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Weather sample code using QNetworkRequest


nemory wrote:

try check if it connected the signal and slot 

 

bool connected = connect(temperReply, SIGNAL(finished()), this, SLOT(onFinishedTemperature()));

 

qDebug() << "CONNECTED: " << connected;


better this way:

 

bool connected = ......

Q_ASSERT(connected);

 

Q_ASSERT will stop the app - but only in DEBUG mode.

it's more safe this way - sometimes there are so many debug messages you perhaps overlooking it

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
msalamali
Posts: 39
Registered: ‎09-09-2013
My Device: Dev Alpha C

Re: Weather sample code using QNetworkRequest

it is connected and i added the server info but still receiving "Unknown Error" and empty reply (error code 0)

Be like Dan Brown, get the dimensions of every place you visit!
MeasureMe on App World
Developer
msalamali
Posts: 39
Registered: ‎09-09-2013
My Device: Dev Alpha C

Re: Weather sample code using QNetworkRequest

[ Edited ]

finally worked by calling "connect" before "get" and passing the network access manager (not the reply) object to the connect function as in this link:

 

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

 

Be like Dan Brown, get the dimensions of every place you visit!
MeasureMe on App World