I started with this example, i want by this to display the link and number of views on youtube but i can't :
testWebService.h
#ifndef TESTWEBSERVICE_H
#define TESTWEBSERVICE_H
#include <QtWidgets/QMainWindow>
#include "ui_testwebservice.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#include <QUrl>
{
Q_OBJECT
public:
TestWebService
(QWidget *parent
= 0);
~TestWebService();
private slots:
void onResult(QNetworkReply* reply);
private:
Ui::TestWebServiceClass ui;
QNetworkAccessManager m_networkManager;
QNetworkReply* m_currentReply;
};
#endif // TESTWEBSERVICE_H
#ifndef TESTWEBSERVICE_H
#define TESTWEBSERVICE_H
#include <QtWidgets/QMainWindow>
#include "ui_testwebservice.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#include <QUrl>
class TestWebService : public QMainWindow
{
Q_OBJECT
public:
TestWebService(QWidget *parent = 0);
~TestWebService();
private slots:
void onResult(QNetworkReply* reply);
private:
Ui::TestWebServiceClass ui;
QNetworkAccessManager m_networkManager;
QNetworkReply* m_currentReply;
};
#endif // TESTWEBSERVICE_H
To copy to clipboard, switch view to plain text mode
testWebService.cpp
#include "testwebservice.h"
TestWebService
::TestWebService(QWidget *parent
){
ui.setupUi(this);
QUrl url
("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json");
QNetworkRequest request;
request.setUrl(url);
m_currentReply = NULL;
m_currentReply = m_networkManager.get(request); // GET
connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));
}
TestWebService::~TestWebService()
{
}
void TestWebService::onResult(QNetworkReply* reply)
{
if (m_currentReply->error() != QNetworkReply::NoError)
return; // ...only in a blog post
QScriptEngine engine;
QScriptValue result = engine.evaluate(data);
// Now parse this JSON according to your needs !
QScriptValue entries = result.property("feed").property("entry");
QScriptValueIterator it(entries);
while (it.hasNext()) {
it.next();
QScriptValue entry = it.value();
QString link
= entry.
property("content").
property("src").
toString();
int viewCount = entry.property("yt$statistics").property("viewCount").toInteger();
// Do something with those...
}
}
#include "testwebservice.h"
TestWebService::TestWebService(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QUrl url("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json");
QNetworkRequest request;
request.setUrl(url);
m_currentReply = NULL;
m_currentReply = m_networkManager.get(request); // GET
connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));
}
TestWebService::~TestWebService()
{
}
void TestWebService::onResult(QNetworkReply* reply)
{
if (m_currentReply->error() != QNetworkReply::NoError)
return; // ...only in a blog post
QString data = (QString) reply->readAll();
QScriptEngine engine;
QScriptValue result = engine.evaluate(data);
// Now parse this JSON according to your needs !
QScriptValue entries = result.property("feed").property("entry");
QScriptValueIterator it(entries);
while (it.hasNext()) {
it.next();
QScriptValue entry = it.value();
QString link = entry.property("content").property("src").toString();
int viewCount = entry.property("yt$statistics").property("viewCount").toInteger();
// Do something with those...
}
}
To copy to clipboard, switch view to plain text mode
I don't get link and viewCount , does anyone know where is the problem in my code ?
Bookmarks