reading from NetworkReply always give incomplete string of data
-
void WebApi::readReady() { QFile file("response.json"); file.open(QIODevice::WriteOnly); QTextStream stream(&file); stream << reply->readAll(); qDebug() << reply->readAll(); stream.flush(); }
This code always results in such a response
// A lot more data here but still cutoff -87dd-415e-bbc0-2ec27bf404cc", "type": "tag", "attributes": { "name": { "en": "Fantasy" }, "description": {}, "group": "genre", "version": 1 }, "relationships": [] } ], "state": "published", "chapterNumbersResetOnNewVolume": false, "createdAt": "2019-02-09T21: 22: 51+00: 00", "updatedAt": "2023-03-10T19: 04: 19+00: 00", "version": 8, "availableTranslatedLanguages": [ "en", "cs", "ru" ], "latestUploadedChapter": "f729402f-5d8f-4bb4-832a-842e0fa4da9e" }, "relationships": [ { "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2", "type": "author" }, { "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2", "type": "artist" }, { "id": "81af78b3-9a6c-4fb9-8add-348b92275b9e", "type": "cover_art" } ] } ], "limit": 10, "offset": 0, "total": 2 }
why is it so?
where am i going wrong? -
@Ragbot
This issue is not to do with design, and belongs in the General forum.You cannot just call
reply->readAll()
and expect it to return something at the instant called. You need to attach a slot for when data arrives. And your data may well arrive in multiple "chunks", not all in one call, so the signal/slot may be called multiple times. IfreadReady()
is a slot (who knows?) it needs to allow to be called several times, e.g. it can't just overwrite an output file.Also do not call
qDebug() << reply->readAll();
, that would do a new read, which I doubt you intend. -
J J.Hilk moved this topic from Qt Design Studio on
-
@JonB Thanks I didn't mean to publish it to the design section sorry.
and yes it is connected via
reply = client.get(QNetworkRequest(QUrl(base_url + page))); connect(reply, &QNetworkReply::readyRead, this, &MangaDex::readReady);
and I do understand the chunking concept but I am not sure how to finish writing those chunks so I don't just keep appending to the previous response.
How do handle this I?
Is there a way to know when it is finished?
if so how do I use it in this context? -
@Ragbot said in reading from NetworkReply always give incomplete string of data:
how to finish writing those chunks so I don't just keep appending to the previous response
You need some kind of protocol, so you know when you received a whole package...
-
@Ragbot
Yes, assuming what you are getting is just JSON content, see void QNetworkReply::finished(). You can also do areadAll()
there to read and save reply in one call. Presumably Ok unless package data is huge. Try just moving yourreadReady()
to that signal?