json - How to manage QNetworkReply order from QNetworkAccessManager requests -


i have demo server replying json objects client request.

i planning use qnetworkaccessmanager client, did.

i defined lambda function handling server reply

        std::function<void(qnetworkreply*)> processreplylb = [&](qnetworkreply *reply){         static int cnt = 0;         std::cout<<"--------------------"<<(++cnt)<<"---------------------"<<std::endl;         qlist<qbytearray> headerlist = reply->rawheaderlist();         foreach (qbytearray header, headerlist) {             std::cout<<header.constdata()<<" - "<<reply->rawheader(header).constdata()<<std::endl;         }          processresult = false;         if(reply->error()){             std::cout<<"reply error"<<std::endl;             std::cout<<reply->errorstring().toutf8().constdata()<<std::endl;         } else {             qstring value = reply->readall();             std::cout<<"value = "<<value.toutf8().constdata()<<std::endl;             qjsondocument doc = qjsondocument::fromjson(value.toutf8());             if(doc.isnull()){                 std::cout<<"json document null"<<std::endl;             }else if(doc.isempty()){                 std::cout<<"json document empty"<<std::endl;             } else if(!doc.isobject()){                 std::cout<<"json document not object"<<std::endl;             } else {                 qjsonobject obj = doc.object();                 qstring responsestr = obj.value("result").tostring();                 processresult = (responsestr == "ok");                 if(obj.contains("message")){                     qjsonvalue messagevalue = obj.value("message");                     std::cout<<messagevalue.tostring().toutf8().constdata()<<std::endl;                 }             }         }         reply->deletelater();         std::cout<<"--------------------"<<(cnt)<<"---------------------"<<std::endl;     }; 

and connected lambda slot qnetworkaccessmanager in 2 functions used

  1. check if client session exist on server(sends request).
  2. login using id , password (send post request parameters).

in main function, if invoke checksession() or login() respectively, result fine. if try call

login(); checksession(); 

in sequence, lambda invoked 4 times checksession() result came first, following null json, login json result , null json.

i know qnetworkaccessmanager works asynchronously, eventloop can solve problem, not applicable in real development mode due writing client background service component.

so how can design client can make sure login result processed before checksession?

btw, used java servlet server without asynchronous. trivial doget , dopost processes.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -