I am building a app that contacts the server for data, gets it and then does some processing before displaying the result.
I have created a mapping function that maps all the functions that need to be called if a particular request has to be successful.
This code is the main part where all the calling is done.
if (check_if_clientID_is_present())
call = "teci.create.client";
qDebug() << "call"<< call;
if(!call.isEmpty() && API_Request_Pointer == false)
mapping(call);
if (check_if_clientID_is_present())
call = "teci.create.client";
qDebug() << "call"<< call;
if(!call.isEmpty() && API_Request_Pointer == false)
mapping(call);
To copy to clipboard, switch view to plain text mode
This is the mapping code where the required functions are called.
First i prepare the xml document, call the fetch function, then wait for the readyread and requestfinished signals, then call the parsing function and then exit the mapping function
void MainWindow
::mapping(QString callname
) {
qDebug() <<"in mapping";
if(call.compare("teci.create.client",callname)== 0)
{
qDebug() <<"inside create client";
API_Request_Pointer = true;
create_client();
fetch();
connect(&http, SIGNAL(requestFinished(int, bool)),
this, SLOT(finished(int, bool)));
parse_create_client();
API_Request_Pointer = false;
qDebug() <<"leaving create client";
}
else if (call.compare("teci.test.reverse",callname)== 0)
{
API_Request_Pointer = true;
reverse_string();
fetch();
connect(&http, SIGNAL(requestFinished(int, bool)),
this, SLOT(finished(int, bool)));
parse_reverse_string();
API_Request_Pointer = false;
}
qDebug() <<"leaving mapping";
}
void MainWindow::mapping(QString callname)
{
qDebug() <<"in mapping";
QThread thread;
if(call.compare("teci.create.client",callname)== 0)
{
qDebug() <<"inside create client";
API_Request_Pointer = true;
create_client();
fetch();
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
this, SLOT(readData(const QHttpResponseHeader &)));
connect(&http, SIGNAL(requestFinished(int, bool)),
this, SLOT(finished(int, bool)));
parse_create_client();
API_Request_Pointer = false;
qDebug() <<"leaving create client";
}
else if (call.compare("teci.test.reverse",callname)== 0)
{
API_Request_Pointer = true;
reverse_string();
fetch();
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
this, SLOT(readData(const QHttpResponseHeader &)));
connect(&http, SIGNAL(requestFinished(int, bool)),
this, SLOT(finished(int, bool)));
parse_reverse_string();
API_Request_Pointer = false;
}
qDebug() <<"leaving mapping";
}
To copy to clipboard, switch view to plain text mode
void MainWindow::fetch()
{
xml.clear();
string.append(":");
string.append("12345");
QString encoded
= string.
toBase64();
qDebug() <<"String"<< string <<"Encoded" <<encoded ;
QUrl url
("http://..../");
url.setUserName("username");
url.setPassword("12345");
header.setValue("Host",url.host());
header.setValue("Path",url.path());
header.setValue("Authorization","Basic " + encoded);
http.setHost(url.host(),url.port(80));
connectionId = http.request(header,xmlrequest);
qDebug() << "Leaving fetch";
}
void MainWindow::fetch()
{
xml.clear();
QByteArray string ("username");
string.append(":");
string.append("12345");
QString encoded = string.toBase64();
qDebug() <<"String"<< string <<"Encoded" <<encoded ;
QUrl url("http://..../");
url.setUserName("username");
url.setPassword("12345");
QHttpRequestHeader header("POST","/api/");
header.setValue("Host",url.host());
header.setValue("Path",url.path());
header.setValue("Authorization","Basic " + encoded);
http.setHost(url.host(),url.port(80));
connectionId = http.request(header,xmlrequest);
qDebug() << "Leaving fetch";
}
To copy to clipboard, switch view to plain text mode
void MainWindow::create_client()
{
here i add tags and create the xml
xmlrequest.append(doc.toString());
qDebug() << xmlrequest.data();
void MainWindow::create_client()
{
here i add tags and create the xml
xmlrequest.append(doc.toString());
qDebug() << xmlrequest.data();
To copy to clipboard, switch view to plain text mode
{
if (resp.statusCode() != 200)
http.abort();
else
{
xml.addData(http.readAll());
}
qDebug() << "Leaving readdata";
}
void MainWindow::readData(const QHttpResponseHeader &resp)
{
if (resp.statusCode() != 200)
http.abort();
else
{
xml.addData(http.readAll());
}
qDebug() << "Leaving readdata";
}
To copy to clipboard, switch view to plain text mode
void MainWindow::finished(int id, bool error)
{
qDebug() <<"Error" <<http.errorString();
if (error)
qWarning("Received error during HTTP fetch.");
qDebug() << "Leaving finished";
}
void MainWindow::finished(int id, bool error)
{
qDebug() <<"Error" <<http.errorString();
if (error)
qWarning("Received error during HTTP fetch.");
qDebug() << "Leaving finished";
}
To copy to clipboard, switch view to plain text mode
void MainWindow::parse_create_client()
{
qDebug() <<"Entering parser";
while (!xml.atEnd())
{
xml.readNext();
qDebug()<<xml.readNext();
if (xml.isStartElement())
{
currentTag = xml.name().toString();
if (currentTag == "client")
API_key += xml.attributes().value("key").toString();
qDebug() << "CurrentTag"<<currentTag;
}
else if (xml.isCharacters() && !xml.isWhitespace())
{
if (currentTag == "password")
API_password += xml.text().toString();
}
}
qDebug() << "webiste_clientID"<< API_key;
qDebug() << "webiste_serial no"<<API_password;
}
}
void MainWindow::parse_create_client()
{
qDebug() <<"Entering parser";
while (!xml.atEnd())
{
xml.readNext();
qDebug()<<xml.readNext();
if (xml.isStartElement())
{
currentTag = xml.name().toString();
if (currentTag == "client")
API_key += xml.attributes().value("key").toString();
qDebug() << "CurrentTag"<<currentTag;
}
else if (xml.isCharacters() && !xml.isWhitespace())
{
if (currentTag == "password")
API_password += xml.text().toString();
}
}
qDebug() << "webiste_clientID"<< API_key;
qDebug() << "webiste_serial no"<<API_password;
}
}
To copy to clipboard, switch view to plain text mode
After reading the data, i write it down in a file.
This is the output i am getting.
Getting data from the webiste
call "teci.create.client"
in mapping
inside create client
<request version="0.1" >
<call name="teci.client.create" >
<client>
<kind>MOBILE</kind>
<name>API Test suite 7001</name>
</client>
</call>
</request>
Leaving fetch
Entering parser
1
webiste_clientID ""
webiste_serial no ""
leaving create client
leaving mapping
Leaving finished
Leaving readdata
Getting data from the webiste
call "teci.create.client"
in mapping
inside create client
<request version="0.1" >
<call name="teci.client.create" >
<client>
<kind>MOBILE</kind>
<name>API Test suite 7001</name>
</client>
</call>
</request>
Leaving fetch
Entering parser
1
webiste_clientID ""
webiste_serial no ""
leaving create client
leaving mapping
Leaving finished
Leaving readdata
To copy to clipboard, switch view to plain text mode
the readyread and requestfinished signals are being called after the parsing function has been called.
How can i wait for the signal and then call the parsing function??
Bookmarks