I'm working on a project required me to post JSON request to API server then receive data from server. However I cannot make it work. This is my code
MainWindow
::MainWindow(QWidget *parent
) :ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl serviceUrl
= QUrl("https://www.jusmine.jp/KA/KGBloginCheck");
QNetworkRequest request(serviceUrl);
QJsonObject json;
json.insert("userid","xxxx");
json.insert("userpass","xxxx");
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.
setHeader(QNetworkRequest
::ContentLengthHeader,
QByteArray::number(json.
size()));
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(serviceRequestFinish(QNetworkReply*)));
networkManager->post(request,QJsonDocument(json).toJson());
}
void MainWindow::serviceRequestFinish(QNetworkReply *rep)
{
qDebug()<<"Test: "<<strReply;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl serviceUrl = QUrl("https://www.jusmine.jp/KA/KGBloginCheck");
QNetworkRequest request(serviceUrl);
QJsonObject json;
json.insert("userid","xxxx");
json.insert("userpass","xxxx");
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(json.size()));
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(serviceRequestFinish(QNetworkReply*)));
networkManager->post(request,QJsonDocument(json).toJson());
}
void MainWindow::serviceRequestFinish(QNetworkReply *rep)
{
QString strReply = (QString)rep->readAll();
qDebug()<<"Test: "<<strReply;
}
To copy to clipboard, switch view to plain text mode
After running the program I get nothing from server. (This code is built on Qt 4.7.0 so I have to use library qjson4-master). I don't know if any one can help me in this problem.
This is the result of testing server API:
Untitled.jpg
Thank you!
Bookmarks