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: 120
Registered: ‎12-22-2011
My Device: ---
My Carrier: ---
Accepted Solution

BlackBerry Http Connection Encoding Issue

Dears ,

 

I connected to http url using :

 

QUrl mRequestParams = QUrl(url);

	QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
	QUrl requestUrl(url);

	QNetworkRequest request(requestUrl);
	request.setHeader(QNetworkRequest::ContentTypeHeader,
			"application/x-www-form-urlencoded");

	if (networkManager) {

		bool resFromServer = connect(networkManager,
				SIGNAL(finished(QNetworkReply*)), this,
				SLOT(requestFinished(QNetworkReply*)));

		qDebug() << "Connection is success : ? : " << resFromServer;

		Q_ASSERT (resFromServer);
		Q_UNUSED(resFromServer);

		QNetworkReply *reply = networkManager->post(request,
				mRequestParams.encodedQuery());
		if (reply) {
			qDebug() << "Reply from server is " << reply;
		}
	}

 

	/*!
	 * @brief QString object to be used to store server response
	 */
	QString response;

	if (reply) {
		if (reply->error() == QNetworkReply::NoError) {
			int available = reply->bytesAvailable();
			if (available > 0) {
				QByteArray buffer(reply->readAll());
				response = QString(buffer);
			} else
				response = "Zero bytes in response";

		} else {
			int httpStatus = reply->attribute(
					QNetworkRequest::HttpStatusCodeAttribute).toInt();
			response =
					"Error and the code is "
							+ reply->attribute(
									QNetworkRequest::HttpStatusCodeAttribute).toString()
							+ "  And the Error string is :: "
							+ reply->errorString() + "\n";
		}
		reply->deleteLater();
	} else {
		response = "Response comes out to be null";
	}

	if ((response.trimmed().isEmpty())) {
		response = "Empty Response";
	}
	qDebug() << "Response String is :: " << response << " \n";

	if (!response.isNull()) {
		// You got response . Do whatever you want to do with it.
		QTextCodec *codec = QTextCodec::codecForName("UTF-8");
		QByteArray encodedString = codec->fromUnicode(response);

		parseXml(encodedString);
	}

 

 

void ListChannelModel::parseXml(QByteArray xmlByte) {

	/* QXmlStreamReader takes any QIODevice. */
	QXmlStreamReader xml(xmlByte);

	if (xml.hasError()) {
		qDebug() << "XML error: " << xml.errorString() << endl;

	} else {

		XmlDataAccess xda;
		QVariant list = xda.loadFromBuffer(xmlByte, "channels/channel");

		ArrayDataModel *dataModel = new ArrayDataModel();

		dataModel->append(list.value<QVariantList>());

		emit dataLoadedSignal(dataModel);
	}

 

after i loaded the data into list view i face encoding error 

check the image

 

BB_10_Encoding_issue_image.png

 

Can you help me ?

 

 

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: BlackBerry Http Connection Encoding Issue

What is the encoding used in the reply?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 120
Registered: ‎12-22-2011
My Device: ---
My Carrier: ---

Re: BlackBerry Http Connection Encoding Issue

UTF-8

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: BlackBerry Http Connection Encoding Issue

Have you tried using the QString::fromUtf8 method?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 120
Registered: ‎12-22-2011
My Device: ---
My Carrier: ---

Re: BlackBerry Http Connection Encoding Issue

Thanks.