c++ - How to use QNetworkManager for REST api? -
i make class accessing data via rest api, example:
class meteostation{ int getlatesttemperature(); int getlatestpessure(); private: qnetworkmanager nmng; }
how implement methods? using like:
int meteostation::getlatesttemperature(){ ... qeventloop eventloop; connect(&m_nam,signal(finished(qnetworkreply*)),&eventloop,slot(quit())); qnetworkreply *reply = m_nam.get( req ); eventloop.exec(); reply->readall() ... }
but since using inner qeventloop not recommended, how should see whom response belong to?
meteostation::meteostation(){ connect(&nmam, signal(finished(qnetworkreply*)), this, slot(parsenetworkresponse(qnetworkreply*))); } void meteostation::parsenetworkresponse( qnetworkreply *finished ) { qbytearray data = finished->readall(); ...
yes , nice have class thread save. how solving in code?
how bad making call synchronous with:
qnetworkrequest req(url); qscopedpointer<qnetworkreply> reply(nam.get(req)); qtime timeout= qtime::currenttime().addsecs(10); while( qtime::currenttime() < timeout && !reply->isfinished()){ qcoreapplication::processevents(qeventloop::allevents, 100); } if (reply->error() != qnetworkreply::noerror) { qdebug() << "failure" <<reply->errorstring(); } qbytearray data = reply->readall();
i've resolved problem using qcoreapplication::processevents(). response there within ms , i'm able implement functionality close libcurl.
qnetworkrequest req(url); qscopedpointer<qnetworkreply> reply(nam.get(req)); qtime timeout= qtime::currenttime().addsecs(10); while( qtime::currenttime() < timeout && !reply->isfinished()){ qcoreapplication::processevents(qeventloop::allevents, 100); } if (reply->error() != qnetworkreply::noerror) { qdebug() << "failure" <<reply->errorstring(); } qbytearray data = reply->readall();
Comments
Post a Comment