PDA

View Full Version : QNetworkAccessManager not receiving data in Android, but works in Desktop



kubark42
7th November 2014, 05:51
I have a very simple GET request, which works fine in Desktop Qt, but no data is returned when I run it on Android:



QNetworkManager m_WebCtrl;
QNetworkReply reply;
QUrl url("http://www.google.com");
request.setUrl(url);
reply = m_WebCtrl.get(request);

// Infinite loop to watch the data come in
while (1) {
qDebug()<< "Data! " << reply->bytesAvailable();
}


The Android device has an IP address that is in the valid range. I don't see any errors with reply->error(). What can this be related to?

Edit: I should add that I'm using Qt 5.4 beta.

anda_skoa
7th November 2014, 09:00
That should not work on the desktop either, your while loop is blocking the event loop.

You don't need the while loop, just connect to the network reply's readyRead() signal.

Cheers,
_

kubark42
7th November 2014, 14:45
That should not work on the desktop either, your while loop is blocking the event loop.

You don't need the while loop, just connect to the network reply's readyRead() signal.




Thanks for the comment. I'm guessing the stripped down version I showed above works because of the time it takes to print the qDebug() statement. But I updated the original code sample with a QEventLoop for clarity.

The full code uses the readyRead() signal, but I observe identical behavior. The readyRead() signal is never called.

=====================
New code with QEventLoop:


QNetworkManager m_WebCtrl;
QNetworkReply reply;
QUrl url("http://www.google.com");
request.setUrl(url);
reply = m_WebCtrl.get(request);

// Infinite loop to watch the data come in
while (1) {
QEventLoop eventloop;
QTimer::singleShot(1000, &eventloop, SLOT(quit()));
eventloop.exec();

qDebug()<< "Data! " << reply->bytesAvailable();
}

anda_skoa
8th November 2014, 09:54
Why do you have a timer instead of connecting to the signal you are waiting for (readyRead)?
Or even connect to the finished() signal if you want to proceed once the full response has been received?

Cheers,
_

kubark42
23rd November 2014, 16:49
Why do you have a timer instead of connecting to the signal you are waiting for (readyRead)?
Or even connect to the finished() signal if you want to proceed once the full response has been received?
_

I guess I could have been clearer in my prior two posts when I said that the full code properly uses sockets.

I've tracked the problem down to the individual piece of hardware. The true URL I've been trying to access is http://192.168.122.1:8080/liveview/liveviewstream, but of course I don't figure anyone here has a Sony Camera handy. What's puzzling me is that the Android device can access the camera just fine, it's only Qt that has a problem.

This is getting harder to debug, since it certainly looks like a bug (works with Sony Camera API on Qt Desktop, works with Android Chrome, works with Sony's java app), but I'm unsure how to post a minimal code set that can be tested.

What I have noticed as well is that sometimes non-standard ports cause the QNetworkReply to fail. It's intermittent on Qt Android, but never fails on Qt Desktop. For instance, replacing http://google.com with http://open.zorinaq.com:1337/ in the above code leads to frequent failures. Might be related.

anda_skoa
23rd November 2014, 17:19
What I have noticed as well is that sometimes non-standard ports cause the QNetworkReply to fail. It's intermittent on Qt Android, but never fails on Qt Desktop. For instance, replacing http://google.com with http://open.zorinaq.com:1337/ in the above code leads to frequent failures. Might be related.

Maybe, now that you have a publicly usable URL to test with, you could post code that triggers the problem for you and is not using some obscure while loop hack that your actual program is not using anyway.

Cheers,
_

P.S.: since you encounter your problem on a system with an access restrictions system, have you verified that your program has actually been granted the permissions you need?