重构为独立类后,QNetworkRequest (HTTP GET) 不会触发

发布于 2024-10-16 00:39:20 字数 2328 浏览 0 评论 0原文

我最近开始了对大约两个月前编写的大型整体音频播放器应用程序进行模块化的繁琐过程。

这个过程进展得相当顺利,尽管其中一种方法(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 下针对 Qt 库版本 4.7.0 的 MinGW 版本(包含在 Qt SDK 2010.05 中)构建应用程序本身7x86-64。

任何帮助将不胜感激。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-10-23 00:39:20

在阅读了许多不同的信息来源(其中大多数是矛盾的)之后,我发现以下结果产生了一个可行的解决方案 - 尽管调试输出中存在一个外观问题,但似乎并不影响操作(Object::connect:没有这样的信号 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

感谢大家的帮助。

希望这段代码将来能够作为其他人的有用模板。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文