QNetworkReply不触发

时间:2013-09-29 02:06:14

标签: c++ qt qnetworkaccessmanager qobject qnetworkreply

我是C ++和Qt的新手,所以很自然我遇到了麻烦。我正在尝试创建一个独立于MainWindow的类,它连接到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;
}

如果你们想出任何事情,请告诉我。

1 个答案:

答案 0 :(得分:1)

更好的连接方式

QNetworkReply *reply = manager->post (request, data);
connect (reply,
    SIGNAL (finished ()),
    this,
    SLOT (handleReplyFinished ()));