General xmlhttprequest element



  • Hi,
    I am new to the subject of HTTP, so my questions might seem trivial for some of you.

    I need an element which handles all the http requests in my app.
    It shold be able to handle GET and POST requests
    The requests come from various elements, which require different requests and replies.
    Some elements send several requests in a row.

    1. How can I assure, that all requests from one element are returned to the same element (is there something like QNetworkRequest's originatingObject?) and is there something like an ID that I can attach to the requests?

    2. How can I upload binary-data (with HTTP-POST?)

    3. How can I make sure that the responses of old requests (that are not needed anymore) are ignored.

    Thanks



  • You just have to create a C++ QObject class (myHttp in the example) and using QNetworkAccessManager to make your Http request !get() and post() are avaible from there.
    Then, you need to add your class in the QML context as a global object accessible from everywhere :

    @class MyHttp : public QObject {
    public:
    Q_INVOKABLE void getMyRequest();

    private:
    QNetworkAccessManager * mNetManager:

    }

    void MyHttp::getMyRequest(){
    mNetManager->get(QNetworkRequest(QUrl("http://......"));
    }
    @

    Then from main.cpp :

    @
    MyHttp myAttp;
    view.rootContext()->setContextProperty("myHttp", &myHttp);
    @

    Then, from QML, you should be able to call invokable method from everywhere :

    @
    MousArea{
    onClicked: myHttp.getMyRequest()
    }@

    Upload binary : "take a look here":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html#post :



  • Thanks for the answer.
    How can I assigin the replies to the sender of the request from qml and how to merge several replies (text-snippets) to the same origin?



  • @
    QNetworkReply * reply = mNetManager->get(QNetworkRequest(QUrl("http://......"));
    connect(reply,SIGNAL(finished()),this,SLOT(parseResult()));

    void MyHttp::parseResult()
    {
    QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());

    QString dataReceive = reply->readAll();

    }

    @



  • Thank you very much.
    Things are getting clearer for me now. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.