qt http get和post接口有阻塞和非阻塞两种实现。非阻塞的方式是信号槽关联,在槽函数中实现。
实际项目中多个接口调用的话需要区分调用接口,然后根据接口解析json报文,我分享下我的方法。
通过获取QString url = reply->url().toString(); 可以获取到调用的url,可以根据url来进行区分。
connect(m_managerget,SIGNAL(finished(QNetworkReply*)), this,
SLOT(slot_getReplayFinished(QNetworkReply*))); //关联信号和槽
connect(m_managerpost,SIGNAL(finished(QNetworkReply*)), this,
SLOT(slot_postReplayFinished(QNetworkReply*))); //关联信号和槽
void slot_postReplayFinished(QNetworkReply *reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QByteArray bytes = (reply->readAll());
QString postUrl = reply->url().toString();
if(statusCode != 200)
{
qDebug() << "post replayFinished code:" << statusCode << postUrl << ",data:" << bytes;
return;
}
if(postUrl.indexOf("/linkServer") > 0) //示例接口
{
//进行相应解析
}
return;
}
转载请注明出处,这是查了好些资料和经验积累的成果。
标签:slot,http,qt,get,url,阻塞,接口,QNetworkReply,reply From: https://blog.csdn.net/h892359495/article/details/136863167