dimkanovikov / WebLoader

High level wrapper around QNetworkAccessManager for make network communications easy
GNU Lesser General Public License v3.0
17 stars 6 forks source link

no matching function for call to 'NetworkRequest::loadAsync #4

Closed xeoshow closed 8 years ago

xeoshow commented 8 years ago

Hello, I am trying to write below code:

        NetworkRequest request;
        request.setRequestMethod(NetworkRequest::Post);
        request.setRawRequest(postJsonData.toUtf8(), "application/json");
        request.loadAsync("http://www.webforgood.com/api/v2/updatecloudconfig", [] (const QByteArray& replyData) {
            LOG(TRACE) << "replyData=" << replyData;
        });

But got this error, anything I did wrong? Or just not supported? Thanks! D:\gitrepository\ui-desktop\mainwindow.cpp:572: error: no matching function for call to 'NetworkRequest::loadAsync(const char [47], MainWindow::nativeEvent(const QByteArray&, void, long int)::<lambda(const QByteArray&)>)' });

xeoshow commented 8 years ago

I am trying to combine below two : NetworkRequestLoader::loadAsync(thousandsUrls, [](const QByteArray& _loadedData, const QUrl& _fromUrl) { qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl; });

NetworkRequest request; requset.setRequestMethod(NetworkRequest::Post); request.setRawRequest("{ \"method\": \"getUsers\" }", "application/json"); const QByteArray usersJson = request.loadSync("https://site.com/API/v2/function/");

dimkanovikov commented 8 years ago

Hi,

1) NetworkRequestLoader is a helper to simplify some often used cases. NetworkRequest directly not support this interface. You need doing something like this:

connect(request, &NetworkRequest::downloadComplete, [] (const QByteArray& replyData) {
    LOG(TRACE) << "replyData=" << replyData;
});

2) There is a bad idea to use asynchronous loading method from instance of NetworkRequest on stack. When program will be out of scope in your code NetworkRequest will be destroyed and you'll never get a reply. Better to use dynamically created instance of NetworkRequest like this:

NetworkRequest* request = new NetworkRequest(parent);
request->setRequestMethod(NetworkRequest::Post);
request->setRawRequest(postJsonData.toUtf8(), "application/json");
connect(request, &NetworkRequest::downloadComplete, [] (const QByteArray& replyData) {
    LOG(TRACE) << "replyData=" << replyData;
});
request->loadAsync("http://www.webforgood.com/api/v2/updatecloudconfig");
xeoshow commented 8 years ago

works great! Thanks v much! Really easy to use and lightweight lib for qt! Hope the comments for code could be translated into English as well! :-D

dimkanovikov commented 8 years ago

Thanks for feedback, @xeoshow. Maybe in someday :D