重构为独立类后,QNetworkRequest (HTTP GET) 不会触发
我最近开始了对大约两个月前编写的大型整体音频播放器应用程序进行模块化的繁琐过程。
这个过程进展得相当顺利,尽管其中一种方法(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在阅读了许多不同的信息来源(其中大多数是矛盾的)之后,我发现以下结果产生了一个可行的解决方案 - 尽管调试输出中存在一个外观问题,但似乎并不影响操作(
Object::connect:没有这样的信号 QNetworkReplyImpl::finished(QNetworkReply*) in ../AudioPlayer/scrobblemedia.cpp:29
):在
scrobblemedia.cpp
中:在
中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
:In
scrobblemedia.h
:Thanks to everyone for their help.
Hopefully this code will serve as a useful template for others, in the future.