QNetworkReply 不触发
QNetworkReply Doesn't Trigger
本文关键字:QNetworkReply 更新时间:2023-10-16
我是C++和Qt的新手,所以自然而然地遇到了麻烦。我正在尝试创建一个独立于主窗口的类,该类连接到 URL 并接收 JSON 响应。但是,在使用 QObject::connect 后,程序不会连接到 SLOT 程序。
这是我到目前为止的代码(jsonhandle.h):
#ifndef JSONHANDLE_H
#define JSONHANDLE_H
#include <string>
#include <QObject>
#include <QJsonObject>
#include <QtNetwork/QNetworkReply>
using std::string;
class jsonhandle: public QObject{
Q_OBJECT
public:
jsonhandle(string url);
void sendRequest();
void addArg(string name, string value);
void clearArg();
QJsonObject getResponse();
public slots:
void replyFinished(QNetworkReply * reply);
private:
////////////////////////////////////////////////////////////////////////////////////////////
bool gotResponse; //Determines whether or not a response was obtained.
string url; //The URL where the JSON request takes place.
int size; //The number of parameters.
string *param; //The array containing arguments.
int maxSize; //Keeps track of the top size of the array.
const int DEFAULT = 4; //The default value of the parameter array.
QJsonObject *response; //The object containing the JSON data.
////////////////////////////////////////////////////////////////////////////////////////////
void expandArray(); //Expands the parameter array.
};
#endif // JSONHANDLE_H
jsonhandle.cpp:
#include "jsonhandle.h"
#include <string>
#include <iostream>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QDebug>
using std::string;
jsonhandle::jsonhandle(string url){
//Gets the url and argument list passed to the json handler.
this->url = url;
this->size = 0;
this->maxSize = DEFAULT;
this->gotResponse = false;
this->response = new QJsonObject();
//Now, creates the paramater array.
this->param = new string[DEFAULT];
}
void jsonhandle::addArg(string name, string value){
//Checks to see if the array is full.
if (size >= maxSize){
//The array has to be expanded.
expandArray();
}
//Now, inserts the name and parameter into param.
param[size++] = name;
param[size++] = value;
}
void jsonhandle::expandArray(){
//First gets the size of the array.
int newSize = (this->size) * 2;
//Creates a new array with double the size.
string *newArr = new string[newSize];
//Finally, populates the new array with the parameters.
std::copy(param, param + this-> size, newArr);
//Finally deletes the original param array.
delete[] param;
param = newArr;
//Finally, updates max count.
maxSize *= 2;
}
void jsonhandle::clearArg(){
//Clears the counters.
this->maxSize = DEFAULT;
this->size = 0;
//Deletes the param array off the stack.
delete[] this->param;
this->param = new string[DEFAULT];
}
void jsonhandle::sendRequest(){
//The network manager will post and recieve our HTTP requests.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(replyFinished(QNetworkReply *)));
//The host of the webservice.
QString acceptedURL = QString::fromStdString(this->url);
QUrl url(acceptedURL);
//Sets up the post data request.
QNetworkRequest req;
req.setUrl(acceptedURL);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
//Now, interacts with the data and gets all the additional parameters.
QUrlQuery query(url);
for (int i = 0; i < size; i += 2){
query.addQueryItem(QString::fromStdString(param[i]), QString::fromStdString(param[i + 1]));
}
url.setQuery(query);
//Update the request with the new query information.
req.setUrl(url);
// DEBUG TIP:
// The following line will allow you print the full HTTP request that will be made
// You can enter it directly into a browser, and the JSON object returned will be shown
// This is why iostream is included in this file
std::cout<< url.toString().toStdString()<<std::endl;
//Post the request
manager->post(req, url.toEncoded());
}
void jsonhandle::replyFinished(QNetworkReply * reply){
if (reply->error() != QNetworkReply::NoError){
// A communication error has occurred
std::cout << "Error in connecting." << std::endl;
this->gotResponse = false;
}
//We read the JSON response into a QJsonObject
QJsonObject obj = QJsonDocument::fromJson(reply->readAll()).object();
this->response = &(obj);
this->gotResponse = true;
}
QJsonObject jsonhandle::getResponse(){
//Sees if a response was obtained from JSON
if (gotResponse == false){
response->insert("error",QJsonValue::Null);
}
return *response;
}
如果你们想通了什么,请告诉我。
更好的连接方式
QNetworkReply *reply = manager->post (request, data);
connect (reply,
SIGNAL (finished ()),
this,
SLOT (handleReplyFinished ()));
相关文章:
- QT:管理来自 QNetworkReply 的回复
- QNetworkReply::done() 信号是按顺序调用还是同时调用?
- 将大小限制添加到 QNetworkReply
- 如何从QNetworkReply读取数据?
- 将 QNetworkReply 指针传递给 QSharedPointer
- QNetworkReply 如果 HTTP 响应类型为 201 CREATED,则返回空响应
- Qnetworkreply,QnetworkAccessManager- https下载文件 - 高CPU使用情况
- QnetWorkreply抛出sigsegv时发出的信号
- qnetworkreply粉碎程序以调试模式在ntdll.dll中带有错误
- QNetworkReply 响应是否有数据限制
- QNetworkReply https API 在 Android 设备上不起作用 - QT
- 如何将QNetworkReply信号连接到qt中的进度条
- 如何在没有外部计时器的情况下设置 QNetworkReply 超时
- 什么时候会有QNetworkReply::error信号后跟一个finish()信号
- HTTP请求中的Qnetworkreply和QnetworkAccessManager超时
- Qt-QNetworkReply未显示Content-Length标头
- 如何设置QNetworkReply的内容
- 当再次请求相同的url时,Qt QNetworkReply readAll返回空
- 使用 QNetworkAccessManager 从 web 检索数据:文件已下载,但 QNetworkReply::r
- 获取从 QNetworkReply 下载的百分比