QNetworkRequest (HTTP GET) doesn't fire, after refactoring into a standalone class
I've recently began the tedious process of modularising a large, monolithic audio player application that I wrote roughly 2 months ago.
This process is going reasonably well, although it appears that one of the methods (ScrobbleMedia - which predictably enough makes HTTP requests to submit information about a playing track to last.fm) no longer seems to make network requests.
However, the QUrl object that would be passed through to the QNetworkAccessManager instance/QNetworkRequest is being built correctly.
For comparison, a functional Mercurial revision of the code is available on BitBucket.
The ScrobbleMedia method currently looks like this, after refactoring:
#include "scrobblemedia.h"
#include <QDebug>
#include <cstdio>
ScrobbleMedia::ScrobbleMedia(QString asUsername开发者_开发知识库, QString asPassword,
QString asArtist, QString asTrack, QString asAlbum)
{
QString KEndPointURL = "http://lastfmstats.livefrombmore.com/universalscrobbler/scrobble.php";
QUrl iScrobbleEndPoint(KEndPointURL);
QNetworkAccessManager *iScrobbleDispatcher = new QNetworkAccessManager(this);
iScrobbleEndPoint.addQueryItem("submissionType","track");
iScrobbleEndPoint.addQueryItem("username", asUsername);
iScrobbleEndPoint.addQueryItem("password", asPassword);
iScrobbleEndPoint.addQueryItem("artist", asArtist);
iScrobbleEndPoint.addQueryItem("track", asTrack);
iScrobbleEndPoint.addQueryItem("album", asAlbum);
iScrobbleEndPoint.addQueryItem("number","1");
iScrobbleEndPoint.addQueryItem("duration","200");
iScrobbleDispatcher->get(QNetworkRequest(iScrobbleEndPoint));
connect(iScrobbleDispatcher, SIGNAL(finished(QNetworkReply*)),
SLOT(replyFinished(QNetworkReply*)));
// QString Outside = iScrobbleEndPoint.toEncoded();
qDebug() << "Received: " +
asUsername + " " +
asPassword + " " +
asArtist + " " +
asTrack + " " +
asAlbum;
qDebug() << iScrobbleEndPoint.toString();
}
ScrobbleMedia::~ScrobbleMedia() {
}
The associated header file looks like:
#ifndef SCROBBLEMEDIA_H
#define SCROBBLEMEDIA_H
#include <QString>
#include <QtNetwork>
#include <QUrl>
#include <QNetworkAccessManager>
class ScrobbleMedia : public QObject
{
Q_OBJECT;
private:
public:
ScrobbleMedia(QString asUsername, QString asPassword, QString asArtist, QString asTrack, QString asAlbum);
~ScrobbleMedia();
};
#endif // SCROBBLEMEDIA_H
I'm currently building the application itself against a MinGW build of version 4.7.0 of the Qt libraries (included as part of Qt SDK 2010.05) under Windows 7 x86-64.
Any assistance would be appreciated.
Thanks in advance.
After reading a number of different sources of information (most of which were contradictory), I've found that the following results in a working solution - albeit with a cosmetic issue in the debugging output that doesn't seem to affect operation (Object::connect: No such signal QNetworkReplyImpl::finished(QNetworkReply*) in ../AudioPlayer/scrobblemedia.cpp:29
):
In scrobblemedia.cpp
:
#include "scrobblemedia.h"
#include <QDebug>
#include <cstdio>
ScrobbleMedia::ScrobbleMedia(QString asUsername, QString asPassword,
QString asArtist, QString asTrack, QString asAlbum)
{
QByteArray iDataSink;
QEventLoop iLoop;
QString KEndPointURL = "http://lastfmstats.livefrombmore.com/universalscrobbler/scrobble.php";
QUrl iScrobbleEndPoint(KEndPointURL);
iScrobbleEndPoint.addQueryItem("submissionType","track");
iScrobbleEndPoint.addQueryItem("username", asUsername);
iScrobbleEndPoint.addQueryItem("password", asPassword);
iScrobbleEndPoint.addQueryItem("artist", asArtist);
iScrobbleEndPoint.addQueryItem("track", asTrack);
iScrobbleEndPoint.addQueryItem("album", asAlbum);
iScrobbleEndPoint.addQueryItem("number","1");
iScrobbleEndPoint.addQueryItem("duration","200");
QNetworkAccessManager iScrobbleDispatcher;
QNetworkRequest iScrobbleRequest(iScrobbleEndPoint);
QNetworkReply *iScrobbleReply = iScrobbleDispatcher.get(iScrobbleRequest);
QObject::connect(iScrobbleReply, SIGNAL(finished(QNetworkReply*)), &iLoop,
SLOT(quit()));
iDataSink = iScrobbleReply->readAll();
qDebug() << "Received: " + asUsername + " " + asPassword + " " + asArtist + " " + asTrack + " " + asAlbum;
qDebug() << iScrobbleEndPoint.toString();
iLoop.exec();
}
ScrobbleMedia::~ScrobbleMedia() {
}
void ScrobbleMedia::replyFinished(QNetworkReply*) {
}
void ScrobbleMedia::reallyDone() {
qDebug() << "We've probably successfully Scrobbled...";
}
In scrobblemedia.h
:
#ifndef SCROBBLEMEDIA_H
#define SCROBBLEMEDIA_H
#include <QString>
#include <QtNetwork>
#include <QUrl>
#include <QNetworkAccessManager>
class ScrobbleMedia : public QObject
{
Q_OBJECT
private:
public:
ScrobbleMedia(QString asUsername, QString asPassword, QString asArtist, QString asTrack, QString asAlbum);
~ScrobbleMedia();
private slots:
void replyFinished(QNetworkReply*);
void reallyDone();
};
#endif // SCROBBLEMEDIA_H
Thanks to everyone for their help.
Hopefully this code will serve as a useful template for others, in the future.
精彩评论