Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

QNetworkReply errorString "Premature end of document"

[ Edited ]

Has anyone experienced this issue?  The response comes back correctly for smaller responses (i.e. https://developer.blackberry.com/cascades/files/documentation/images/model.xml), but for larger replies, the response will contain some of the reply data, but not all of it (and I get the "Premature end of document" errorString).  Here is an example URL for a larger response: https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_...

 

The errorString seems to be coming from the QXmlStreamReader but the fact remains that the QNetworkReply has an incomplete response.

 

I've followed the HTTP Communication tutorial almost exactly.  Here is some relevant code:

 

main.cpp:

void ApplicationUI::initiateRequest(QString username, QString password) {
	// Start the activity indicator
	mActivityIndicator->start();

	// Create and send the network request
	QNetworkRequest request = QNetworkRequest();
	QUrl url("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_...");
	request.setUrl(url);
	mNetworkAccessManager->get(request);

}

 

void ApplicationUI::requestFinished(QNetworkReply* reply) {

	// Check the network reply for errors
	if (reply->error() == QNetworkReply::NoError) {

		qDebug() << "\n reply is: " << reply->readAll();

QXmlStreamReader xml;

QMLByteArray data = reply->readAll();
xml.addData(data);

while(!xml.atEnd() && !xml.hasError()) {
// parse here
} /* Error handling. */ if (xml.hasError()) { qDebug() << "\n errorString is: " << xml.errorString(); } xml.clear(); mActivityIndicator->stop(); } else { qDebug() << "\n Problem with the network"; qDebug() << "\n" << reply->errorString(); } }

 

applicationui.hpp:

// Default empty project template
#ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_

#include <bb/cascades/ActivityIndicator>

using namespace bb::cascades;

namespace bb { namespace cascades { class Application; }}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class ApplicationUI : public QObject
{
    Q_OBJECT
public:
    ApplicationUI(bb::cascades::Application *app);

    Q_INVOKABLE void login(QString username, QString password);

    /*!
	 * Initiates the network request.
	 */
	Q_INVOKABLE void initiateRequest(QString username, QString password);

    virtual ~ApplicationUI() {}

private slots:
    /*!
     * Handles the network reply.
     */
    void requestFinished(QNetworkReply* reply);

private:
    ActivityIndicator *mActivityIndicator;
    QNetworkAccessManager *mNetworkAccessManager;
};

#endif /* ApplicationUI_HPP_ */

 

applicationui.cpp

 

// Default empty project template
#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/cascades/ActivityIndicator>

using namespace bb::cascades;

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app)
{

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("app", this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();

	mActivityIndicator = root->findChild<ActivityIndicator*>("indicator");

	// Create a network access manager and connect a custom slot to its
	// finished signal
	mNetworkAccessManager = new QNetworkAccessManager(this);

	bool result = connect(mNetworkAccessManager,
			SIGNAL(finished(QNetworkReply*)),
			this, SLOT(requestFinished(QNetworkReply*)));

	Q_ASSERT(result);
	Q_UNUSED(result);

    app->setScene(root);
}

 

I would appreciate any help!

 

 

 

Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: QNetworkReply errorString "Premature end of document"

My apologies, it looks like I posted a bit prematurely on this one.  The response came back in full.  There were a couple of red herrings that distracted me from the main problems:

 

  1. I was parsing the XML incorrectly
  2. It seems that if the response is too long in the device log, then the device log will trim the message.  Is there a way to configure the device log not to do this?
  3. It looks like the reply message was gone after using the following qDebug() statement.  Will this remove the data from "reply"?  I'm a C++ newbie, coming from Java...
    qDebug() << "\n reply is: " << reply->readAll();