Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Posting multipart http request with QNAM and getting thread crash
Forum Updated to NodeBB v4.3 + New Features

Posting multipart http request with QNAM and getting thread crash

Scheduled Pinned Locked Moved Mobile and Embedded
8 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strekazoid
    wrote on last edited by
    #1

    I'm trying to post a multipart http request with image to the server. Found this example of doing this: http://blog.ilric.org/2010/11/20/qt-http-post-multi-partupload/

    I am implementing this on Nokia E7 device, and currently I'm getting thread crash:
    @Thread has crashed: A data abort exception has occurred accessing 0x1.@

    Here is the code I'm using:

    @void Uploader::Upload(QString path) {
    QNetworkAccessManager *http;
    QVector<QNetworkReply *> response;
    QFileInfo finfo(path);
    QNetworkRequest r(QUrl("http://myserver.com/postimage.php"));

    QString bound="---------------------------723690991551375838472828858";
    QByteArray data(QString("--"+bound+"\r\n").toAscii());
    
    data += "Content-Disposition: form-data; name=\"text\"\r\n\r\n";
    data += "Text\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    data += "Content-Disposition: form-data; name=\"clientid\"\r\n\r\n";
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    data += "Content-Disposition: form-data; name=\"file\"; filename=\""+finfo.fileName()+"\"\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    data += "Content-Type: image/jpeg\r\n\r\n";
    QFile file&#40;path&#41;;
    if (!file.open(QIODevice::ReadOnly))
        return;
    
    data += file.readAll();
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    r.setRawHeader(QString("Accept-Encoding").toAscii(), QString("gzip,deflate").toAscii());
    r.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
    r.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
    //rep->deleteLater();
    response.push_back(http->post(r, data));
    

    }@

    The whole thing crashes on that last line when doint http->post(). I wonder what might be wrong? Could it be mobile specific?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      strekazoid
      wrote on last edited by
      #2

      Ok, the reason was that http object wasn't initialized :D

      @http=new QNetworkAccessManager(this);@

      1 Reply Last reply
      0
      • N Offline
        N Offline
        njeisecke
        wrote on last edited by
        #3

        Maybe you should also do something like this (depending on your use of Uploader):

        @
        connect(http, SIGNAL(finished(QNetworkReply*)), this, SLOT(deleteLater()));
        @
        or
        @
        connect(http, SIGNAL(finished(QNetworkReply*)), http, SLOT(deleteLater()));
        @

        Otherwise all those QNetworkAccessManager instances will stay in memory until the Uploader object is being deleted.

        Did you consider making QNetworkAccessManager a member of the Uploader class and initialize it in the Uploader constructor to reuse it in each call to Upload?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          strekazoid
          wrote on last edited by
          #4

          Thanks, good hint. I'll do that deleteLater().

          I'm currently having this QNAM as a class member, just copied it into the method for this question.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            njeisecke
            wrote on last edited by
            #5

            If you have it as a class member you should not use the QNAM's deleteLater slot as it will be automatically deleted with its parent.

            But you should take care that you don't delete the Uploader object before all running transfers have finished. Otherwise you'll cancel them.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              strekazoid
              wrote on last edited by
              #6

              Thanks, good point!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                strekazoid
                wrote on last edited by
                #7

                By the way, any idea how to get a JSON object out of QNetworkReply? My server is suppose to return a JSON response on success, and I'm trying to figure out if it is in the QNetworkReply, and where exactly?

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  njeisecke
                  wrote on last edited by
                  #8

                  QNetworkReply inherits from QIODevice. So can can simple use readAll to get the UTF-8 encoded string as a QByteArray.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved