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
Highlighted
Developer
Posts: 610
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use
Accepted Solution

Posting Text from BB10 Cascades App to Server

How do you POST code from your Cascades app to server? QHttp or CURL? Samples?

 

1) Collect text from input fields

2) ?? code

3) Post to server

 

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Posting Text from BB10 Cascades App to Server

http://bit.ly/TvdnS0 Smiley Happy
Like all of my posts
Developer
Posts: 610
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Posting Text from BB10 Cascades App to Server

Unfortunately Google does not provide CONTEXT. It only spits out links.

 

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Posting Text from BB10 Cascades App to Server

http://stackoverflow.com/questions/2214595/how-can-i-create-a-http-post-request-with-qt-4-6-1

 

Context? your question has all the context, you want to post data to a server. You already have the data, you just need to post it. Qt how to's seem like enough info.

Like all of my posts
Developer
Developer
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: Posting Text from BB10 Cascades App to Server

here's the cascdes doc for http connections:

https://developer.blackberry.com/cascades/documentation/device_comm/networking/index.html

 

basically, it uses Qt Network Access Manager to get the job done....you can also look at the equivalent Qt doc here:

http://qt-project.org/doc/qt-4.8/QNetworkAccessManager.html

 

Or if you want to avoid c++ and send from javascript/QML:

http://qt-project.org/doc/qt-4.8/qdeclarativeglobalobject.html#xmlhttprequest

 

Developer
Posts: 292
Registered: ‎07-25-2008
My Device: Passport

Re: Posting Text from BB10 Cascades App to Server

[ Edited ]

You already got your answer.

 

The StackOverflow link shows the QNetworkAccessManager API, which is the simple Qt way of using HTTP.  Read up on it, search for more examples if you need to.  Your question is sufficiently generic that an equally generic response is all you're likely to get.

Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: Posting Text from BB10 Cascades App to Server

The simplest implementation of what you are looking for :

Creating connection, request object with added parameters fetched from textfields and then calling PostRequest method

 

/*!
 * @params mRequestParams a QUrl object used to store the parameters used by MyNetwork object while making post request.
 * @params mEmailTxtFld a reference to TextField control used to store the email address entered by user.
 * @params mPassTxtFld a reference to TextField control used to store the password entered by user.
 */
void App::requestNetwork(QUrl mRequestParams, TextField *mEmailTxtFld, TextField *mPassTxtFld)
{
	if (mEmailTxtFld && mEmailTxtFld->text() != NULL
			&& mEmailTxtFld->text().length() > 0)
	{
		qDebug() << "Email value " << mEmailTxtFld->text() << "  \n";
		mRequestParams.addQueryItem("username", mEmailTxtFld->text());
	}
	if (mPassTxtFld && mPassTxtFld->text() != NULL
			&& mPassTxtFld->text().length() > 0)
	{
		qDebug() << "Pass value " << mPassTxtFld->text() << "  \n";
		mRequestParams.addQueryItem("password", mPassTxtFld->text());
	}

	try
	{

		/*!
		 * @brief this object manages the whole network transaction(starting from request creation to response acceptance).
		 */
		QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
		/*!
		 * @brief this QUrl object is used as parameter to QNetworkRequest
		 */
		QUrl requestUrl("Your Server Url That Is Accepting Two Parameters");
		/*!
		 * @brief this QNetworkRequest object acts as a parameter to the post() method for making request to server.
		 */
		QNetworkRequest request(requestUrl);
		request.setHeader(QNetworkRequest::ContentTypeHeader,
				"application/x-www-form-urlencoded");
	
	
		if (networkManager)
		{
			/*!
			 * @brief this connect() method connects finished() Signal of QNetworkAccessManager object with the requestFinished() slot in this app context.
			 */
			bool resFromServer = connect(networkManager,
					SIGNAL(finished(QNetworkReply*)), this,
					SLOT(requestFinished(QNetworkReply*)));
			qDebug() << "Connection is success : ? : " << resFromServer;
			Q_ASSERT(resFromServer);
			Q_UNUSED(resFromServer);

			
			/*!
			 * @brief this object accepts the reply from server once request is made.
			 */
			QNetworkReply *reply = networkManager->post(request,
					mRequestParams.encodedQuery());
			if (reply)
			{
				qDebug() << "Reply from server is " << reply;
			}
		}
	}
	catch (bad_alloc& e)
	{
		qDebug() << "Exception : " << e.what() << "\n";
	}
	catch (bad_exception& e)
	{
		qDebug() << "Exception : " << e.what() << "\n";
	}
	catch (...)
	{
		qDebug() << "Unknown Exception Occurred \n";
	}
	
}

 

Collecting reply from server in a slot method and handling it

 

/*!
* this SLOT method collects the reply from  server and handles it 
*/
void MyNetwork::requestFinished(QNetworkReply *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.
	}
	
}

 


Hope it helps.

 

 

 

 

Always Success
Developer
Posts: 610
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Posting Text from BB10 Cascades App to Server

@MFSI

Thank you!
Lots of success to you as well.

Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: Posting Text from BB10 Cascades App to Server

@greenback    Welcome  

Always Success