Hi Forum,
I have a problem, I want to get the response from the serveur if the operation is Ok or not, So I analysed
QNetworkRequest::HttpStatusCodeAttribute
QNetworkRequest::HttpStatusCodeAttribute
To copy to clipboard, switch view to plain text mode
// *** createProject( param ) ***
//Create project using post json
// ...
reply = manager->post(request, dataByteArray);
/* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */
result = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
//Create project using post json
// ...
reply = manager->post(request, dataByteArray);
/* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */
result = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
To copy to clipboard, switch view to plain text mode
// *** slot onFinished() ***
bool Connection::onFinished()
{
codeResponse = false;
qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << "received";
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 201)
{
codeResponse = true;
}
return codeResponse;
}
bool Connection::onFinished()
{
codeResponse = false;
qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << "received";
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 201)
{
codeResponse = true;
}
return codeResponse;
}
To copy to clipboard, switch view to plain text mode
// *** getter ***
bool Connection::getResponse()
{
return codeResponse;
}
bool Connection::getResponse()
{
return codeResponse;
}
To copy to clipboard, switch view to plain text mode
QML code
connection.createProject( param );
rep = connection.getResponse()
console.log("Response: "+rep)
infoAdd.open()
connection.createProject( param );
rep = connection.getResponse()
console.log("Response: "+rep)
infoAdd.open()
To copy to clipboard, switch view to plain text mode
All is OK, but in QML sometimes function getResponse() runs before the slot onFinished() ??
So there is an other way whitout using getResponse() to get response from onFinichid() ?
I tried to use connection.onFinichid() in QML but it executed 2 times :
1st with connection.createProject( param ), 2nd directly connection.onFinichid() == > Response always = false
I try to find an easy way to get the resp without getter getResponse()
Cheers,
Bookmarks