1

我最近开始了将我大约 2 个月前编写的大型单片音频播放器应用程序模块化的繁琐过程。

这个过程进展得相当顺利,尽管其中一种方法(ScrobbleMedia - 可以预见地足以使 HTTP 请求向 last.fm 提交有关播放曲目的信息)似乎不再发出网络请求。

但是,将传递给 QNetworkAccessManager 实例/QNetworkRequest 的 QUrl 对象正在正确构建。

为了比较,BitBucket上提供了代码的功能性 Mercurial 修订版。

ScrobbleMedia 方法目前看起来像这样,重构后:

#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() {

}

相关的头文件如下所示:

#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

我目前正在针对 Windows 7 x86-64 下的 Qt 库(作为 Qt SDK 2010.05 的一部分包含)的 MinGW 版本构建应用程序本身。

任何援助将不胜感激。

提前致谢。

4

1 回答 1

0

在阅读了许多不同的信息来源(其中大部分是相互矛盾的)之后,我发现以下结果是一个可行的解决方案——尽管在调试输出中存在一个看起来不影响操作的外观问题(Object::connect: No such signal QNetworkReplyImpl::finished(QNetworkReply*) in ../AudioPlayer/scrobblemedia.cpp:29):

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...";
}

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

感谢大家的帮助。

希望此代码将来可以作为其他人的有用模板。

于 2011-02-07T21:40:10.477 回答