我的代码出现问题,我需要你帮我一把。我得到了:
step 1
step 2
QSslSocket: cannot resolve TLSv1_1_client_method
QSslSocket: cannot resolve TLSv1_2_client_method
QSslSocket: cannot resolve TLSv1_1_server_method
QSslSocket: cannot resolve TLSv1_2_server_method
QSslSocket: cannot resolve SSL_select_next_proto
QSslSocket: cannot resolve SSL_CTX_set_next_proto_select_cb
QSslSocket: cannot resolve SSL_get0_next_proto_negotiated
step 3
step 4
Invalid parameter passed to C runtime function.
step 3
Invalid parameter passed to C runtime function.
ASSERT: "a && i >= 0 && i < (int)a->length" in file json\qjsonarray.cpp, line 527
QWaitCondition: Destroyed while threads are still waiting
我只是忽略了QSslSocket错误,因为我知道这是因为https请求,但我没有使用它。这是我的代码:
TheMovieDB.cpp
TheMovieDB::TheMovieDB(QWidget *parent):QWidget(parent)
{
qDebug()<<"step 1";
t_api_key="?api_key=******************";
t_api_url="http://api.themoviedb.org/3/";
t_manager = new QNetworkAccessManager;
t_resultID=0;
}
void TheMovieDB::search(const QString &title)
{
qDebug()<<"step 2";
t_title=GeneralFunctions::prepareString(title); //on normalise la chaîne (retire les accents)
t_api_query= t_api_url + "search/movie" + t_api_key + "&language=fr&query=" + title;
t_manager->get(QNetworkRequest(QUrl(t_api_query)));
QObject::connect(t_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(readTitleSearch(QNetworkReply *)));
}
void TheMovieDB::readTitleSearch(QNetworkReply *reply)
{
qDebug()<<"step 3";
QString source = reply->readAll();
QJsonDocument document = QJsonDocument::fromJson(source.toUtf8());
QJsonObject jsonObj = document.object();
QJsonArray obj = jsonObj["results"].toArray();
QDate date;
int distance=GeneralFunctions::distanceDL(t_title , obj[0].toObject()["title"].toString());
t_resultID=obj[0].toObject()["id"].toInt();
int distanceToBeCompared=0;
for(int i = 1; i < obj.count(); i++){
if(date.fromString(obj[i].toObject()["release_date"].toString(), "yyyy-MM-dd") < QDate::currentDate())
{
distanceToBeCompared=GeneralFunctions::distanceDL(t_title , obj[i].toObject()["title"].toString());
if(distance > distanceToBeCompared)
{
distance=distanceToBeCompared;
t_resultID=obj[i].toObject()["id"].toInt();
}
}
}
searchID(t_resultID);
}
void TheMovieDB::searchID(const int &id)
{
qDebug()<<"step 4";
t_api_query=t_api_url+"movie/"+QString::number(id)+t_api_key+"&language=fr";
t_manager->get(QNetworkRequest(QUrl(t_api_query)));
QObject::connect(t_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(readIDSearch(QNetworkReply *)));
}
void TheMovieDB::readIDSearch(QNetworkReply *reply)
{
qDebug()<<"step 5";
/* some code */
emit idSearchDone();
}
TheMovieDB.h
class TheMovieDB:public QWidget
{
Q_OBJECT
public:
TheMovieDB(QWidget *parent=0);
void search(const QString &title);
QMap<QString, QVariant> t_infosList;
void searchID(const int &id);
private:
QString t_title;
QString t_api_url;
QString t_api_key;
QString t_api_query;
int t_resultID;
QString t_picturePath;
QNetworkAccessManager* t_manager;
public slots:
void readTitleSearch(QNetworkReply *reply);
void readIDSearch(QNetworkReply *reply);
signals:
void idSearchDone();
};
Main.cpp的
TheMovieDB tmdb;
tmdb.search("avatar");
我应该有1,2,3,4,5步骤。但是我得到了第二步而不是第5步。感谢您的帮助。当我启动它时,该应用程序正在窃听。
答案 0 :(得分:0)
在再次连接之前,我们必须断开先前从readTitleSearch完成的信号。为此,请添加:
t_manager->disconnect(t_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(readTitleSearch(QNetworkReply *)));
再次使用之前。