PDA

View Full Version : Web services with Qt5 ??



hassinoss
18th August 2014, 18:06
Hi,

I want some examples using web services with Qt5 , there is anyone can help me ?

wysota
19th August 2014, 13:18
http://lmgtfy.com/?q=webservice+Qt5

hassinoss
19th August 2014, 17:44
Hi,

I didn't post the question until i passed two days searching on a topic treat web services with Qt5, so if you have something witch will help me i will apreciate it.

anda_skoa
19th August 2014, 21:15
The second link in that search has an introduction to that topic, including a full example in a github repository.

Cheers,
_

ChrisW67
19th August 2014, 22:13
Just in case Google gives you a different result set (because your search history is different):
2. http://qmlbook.org/ch11/index.html
4. http://ynonperek.com/course/qt/web-service.html

hassinoss
21st August 2014, 19:55
Hi all,

What realy i want is display weather informations on a Qt application using the web Service here http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56 , but i don't realy know how to proceed

ChrisW67
21st August 2014, 22:08
There are examples of such a request on the page you linked. You should probably use the POST method (last example) rather than a more involved SOAP request method. You can build the query string manually or using QUrlQuery (5.3) or QUrl (4.8). Build a QNetworkRequest pointing at the web service URL (you can find these in the WSDL linked from that page), set the Content-Type header as per the example, and call QNetworkManager::post().

This is a generic HTTP POST process and there are more than a few examples in these forums and the Qt Project forums.

hassinoss
22nd August 2014, 11:52
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>

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



testWebService.cpp


#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...
}
}



I don't get link and viewCount , does anyone know where is the problem in my code ?

wysota
22nd August 2014, 13:39
I don't get link and viewCount
What do you get then?

hassinoss
22nd August 2014, 15:57
When debuging, i see that the "it" is empty the it doesn't enter in the while

wysota
22nd August 2014, 16:08
When debuging, i see that the "it" is empty the it doesn't enter in the while

What does "data" contain?

hassinoss
22nd August 2014, 16:15
Something like this



{
"version": "1.0",
"encoding": "UTF-8",
"feed": {
..
..
"entry": [{
"title": {
"$t": "Nickelback- When We Stand Together"
},
"content": {
"type": "application/x-shockwave-flash",
"src": "http://www.youtube.com/v/76vdvdll0Y?version=3&f=standard&app=youtube_gdata"
},
"yt$statistics": {
"favoriteCount": "29182",
"viewCount": "41513706"
},
...
...
},
...
...
]
}
}

anda_skoa
22nd August 2014, 17:09
Any specific reason you are not using QJsonDocument for parsing JSON data?
http://qt-project.org/doc/qt-5/qjsondocument.html

Cheers,
_

hassinoss
22nd August 2014, 17:51
No there is no specific reason, i just didn't know it, i tried parsing JSON data with QJsonDocument but it doesn't work


QStringList propertyLinks;
QStringList propertyViews;
if (m_currentReply->error() != QNetworkReply::NoError)
return; // ...only in a blog post

QString data = (QString) reply->readAll();

QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8());
QJsonObject jsonObject = jsonResponse.object();

QJsonArray jsonArrayContents = jsonObject["content"].toArray();
QJsonArray jsonArrayStatistics= jsonObject["yt$statistics"].toArray();

foreach (const QJsonValue & value, jsonArrayContents)
{
QJsonObject obj = value.toObject();
propertyLinks.append(obj["src"].toString());
}

foreach (const QJsonValue & value, jsonArrayStatistics)
{
QJsonObject obj = value.toObject();
propertyViews.append(obj["viewCount"].toString());
}


any problem with my code ??

wysota
22nd August 2014, 19:57
but it doesn't work
Please, never say something "doesn't work" without specifying the details. Such information is really useless.


any problem with my code ??
Yes :)

Even at first glance I can see that in line #6 you are casting a QByteArray to QString only to transform it back (using toUtf8() this time) in line #8.

As for real problems, in line #11 you ask for the object's "content" property while it doesn't have such property. "content" is a property of "entry" which itself is a property of "feed". It is all quite obvious once you look at both your code and your input data.

hassinoss
22nd August 2014, 20:08
Thanx for your reply,
I realy don't know how to parse a json file, also i'm newbie in javascript, so can you give me the code how to get the src and viewCount from the file

wysota
24th August 2014, 08:53
Thanx for your reply,
I realy don't know how to parse a json file, also i'm newbie in javascript, so can you give me the code how to get the src and viewCount from the file

Oh come on. Did you understand my previous post? This has nothing to do with "knowing how to parse a json file", that's what you have QJsonDocument for.

hassinoss
25th August 2014, 16:35
Sorry , now i know what did you mean, thanx