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
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10

Working with HTTP POST

Hi

i have troubles when i sent to my php page

i want to post firstname, email, and password to database

i want to post here

heres my code to post it

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

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


#include <bb/system/SystemToast>

using namespace bb::cascades;

ServerDemo::ServerDemo(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);


    qml->setContextProperty("_app",this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    app->setScene(root);

}


void ServerDemo::doPost()
{
	QNetworkAccessManager connection;


	QUrl url("http://www.rws.rajaspot.com/rs_usermanagementclient.php");

	url.addQueryItem("parameter", "14");

	QNetworkRequest request(url);
	showToast("2");
	QByteArray postParameters;

	    postParameters += "fullname=";
	    postParameters += "value1";
	    postParameters += "&";
	    postParameters += "email=";
	    postParameters += "value2";
postParameters += "password=";
postParameters += "value3";
QNetworkReply *reply = connection.post(request, postParameters); connect(reply, SIGNAL(finished()), SLOT(postFinished())); } void ServerDemo::postFinished(){ QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); if (reply->error() == QNetworkReply::NoError){ // No error QString result = reply->readAll(); showToast(result); } else{ // error occured int errorCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << errorCode << endl << reply->errorString(); } reply->deleteLater(); } void ServerDemo::showToast(QString text) { bb::system::SystemToast toast; toast.setBody(text); toast.setPosition(bb::system::SystemUiPosition::BottomCenter); toast.exec(); }

 i took this example from https://supportforums.blackberry.com/t5/Native-Development/Working-with-server/td-p/2648551/page/2

 

i have the form that insert the fullname, email and password in my main.qml and register button

when i click register button that should post to my server on rajaspot

my problem, how to connect that serverdemo.cpp & .hpp to my main.qml?

please give me some code to main.qml because i dont get the good example for this

thanks

 

 

Please use plain text.
Developer
Benecore
Posts: 540
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....

Re: Working with HTTP POST

[ Edited ]

You can create a signal and emit it when post finished

for example:

....postFinished(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if (reply->error() == QNetworkReply::NoError){
        // No error
        QString result = reply->readAll();
        
        // emit the signal here
        emi postFinished(result);

    }
    else{
    }
}

 and then connect it in main.qml file

 

function handleResponse(response){
    console.log("RESPONSE: ".concat(response))
}


onCreationCompleted:{
    
    // connect to the signal
    MyApp.postFinished.connect(handleResponse)
}

 MyApp is property which you need to set in your app constructor after create a scene document

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

// Here
qml->setContextProperty("MyApp", this);

 Hope it helps :smileywink:

 

BTW: You have an issue in postParameters. You missing a char before other params

QByteArray postParameters;

postParameters += "fullname=";
postParameters += "value1";
postParameters += "&"; postParameters += "email="; postParameters += "value2";
postParameters += "password=";
postParameters += "value3";

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Please use plain text.