December 21, 2011

toho71 toho71
Lab Rat
189 posts

QNetworkaccessmanager - post

Page  
1

Hello
When i try to figure out how to use this QNAM and post I Tried this.

I will fill the bytearray with some id (Numeric) and make it as a list.

It works with one value but not with 2 or more and I recive an Unkown error in all of my cases.

Where is the missing part.

The contact to the url— is all right

I fill up the bytearray in a loop

  1.  QByteArray postData;
  2.  
  3.     for (int i = 0; i < selectedToAck.size(); ++i)
  4.     {
  5. postData.append("m_iFuelSensor=");
  6. postData.append(model->item(index.row(),0)->data(Qt::DisplayRole).toString());

  1. /*
  2.  * connection();
  3.  *
  4.  * param postdata- bytearray
  5.  *
  6.  * try to do a form post with QNetworkAccessmanager
  7. */
  8. void Logs::connection( QByteArray postData)
  9. {
  10.  
  11.     manager = new QNetworkAccessManager(this);
  12.     connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));
  13.  
  14.  
  15.     QUrl url(globalP->getGCUUrl() + "/fuelParams.htm? ");
  16.     reply = manager->post(QNetworkRequest(url),postData);
  17.  
  18. }
  19.  
  20. /*
  21.  * finishedSlot();
  22.  *
  23.  * param replay- QNetworkReply
  24.  *
  25.  * answer of the post metchod
  26. */
  27. void Logs::finishedSlot(QNetworkReply* reply)
  28. {
  29.  
  30.     QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );
  31.  
  32.     QMessageBox msgBox;
  33.     QVariant tempQ = QVariant(statusCodeV.toString());
  34.     msgBox.setText(tempQ.toString() + " " + reply->errorString());
  35.     int ret = msgBox.exec();
  36.  
  37.     // "200 OK" received?
  38.     if (statusCodeV.toInt()==200)
  39.     {
  40.  
  41.         msgBox.setText("Successfull!...");
  42.         int ret = msgBox.exec();
  43.  
  44.         QByteArray bytes = reply->readAll(); // bytes
  45.         const QString string(bytes); // string
  46.  
  47.         QMessageBox msgBox;
  48.         msgBox.setText(string );
  49.         msgBox.exec();
  50.     }
  51.  
  52.     else
  53.     {
  54.         msgBox.setText("UnSuccessfull!...");
  55.         int ret = msgBox.exec();
  56.     }
  57.  
  58.     selectedToAck.clear();
  59.     setActiveAlarms();
  60.  
  61.     delete reply;
  62.     delete manager;
  63.  
  64. }

20 replies

December 21, 2011

peppe peppe
Ant Farmer
1025 posts

Was your loop truncated or you’re missing the “&” to join the arguments?

Also: don’t do that by hand. Use QUrl to build the query string, or QHttpMultipart.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

December 21, 2011

toho71 toho71
Lab Rat
189 posts

I didn’t get that either to work properly.
Do you have an example of that

December 21, 2011

toho71 toho71
Lab Rat
189 posts

I even got this in the debugwindow

X Error: BadWindow (invalid Window parameter) 3 Major opcode: 15 (X_QueryTree) Resource id: 0×165b0ef
X Error: BadWindow (invalid Window parameter) 3 Major opcode: 40 (X_TranslateCoords) Resource id: 0×165b0ef
X Error: BadWindow (invalid Window parameter) 3 Major opcode: 15 (X_QueryTree) Resource id: 0×165c579
X Error: BadWindow (invalid Window parameter) 3 Major opcode: 40 (X_TranslateCoords) Resource id: 0×165c579
X Error: BadWindow (invalid Window parameter) 3 Major opcode: 15 (X_QueryTree) Resource id: 0×165e494
X Error: BadWindow (invalid Window parameter) 3 Major opcode: 40 (X_TranslateCoords) Resource id: 0×165e494

December 21, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

In your (truncated) for cycle you are assigning the same parameter multiple times. Is that what you really want to achieve? I believe you want to concat the values into a single variable.

December 21, 2011

toho71 toho71
Lab Rat
189 posts

i want to post
postData.append(“m_iFuelSensor=”) ;// Value
as a list to the server.

it could be one or many values
not as a singel variabel;

December 21, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

  1.      QByteArray postData;
  2.         postData.append("m_iFuelSensor=");    
  3.         for (int i = 0; i < selectedToAck.size(); ++i)
  4.              postData.append(model->item(index.row(),0)->data(Qt::DisplayRole).toString());

December 21, 2011

toho71 toho71
Lab Rat
189 posts

No “?” ?
Because now all the values are in the variabel and its gonna be a big number.
Shouldn’t i have some sort of delimiter maybe

December 21, 2011

toho71 toho71
Lab Rat
189 posts

More errors
I got the errorcode 302 unknown error when i do my post but it changes the value in the textbox.
if I send one value.
When i changed to get operation I got the errorcode 200 “OK”
If someone have a simple example to show or some great hints to get me go on with this “Problem”
I would be very happy.

December 21, 2011

fluca1978 fluca1978
Ant Farmer
524 posts
toho71 wrote:
No “?” ? Because now all the values are in the variabel and its gonna be a big number. Shouldn’t i have some sort of delimiter maybe

You are right, at each iteration you have to add a delimiter.

December 22, 2011

toho71 toho71
Lab Rat
189 posts

Ok thank you but it still doesn’t work as i expected.
Same problems and no update of the webpage.
Maybe I have to do it in another way becaus when I use ex. Wireshark I dont see the incoming values in my postoperation.
Strange.

December 22, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

What if you try to make a GET request? Just to see if the data is sent or not.

December 22, 2011

toho71 toho71
Lab Rat
189 posts

I got the errorcode 302 unknown error when i do my post but it changes the value in the textbox when i update the webpage and if I send one value?

When i changed to get operation I got the errorcode 200 == “OK”

December 22, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

I would start doing a post request with another tool, let say wget, and see how the request must be done and then reproduce it in Qt.

December 22, 2011

toho71 toho71
Lab Rat
189 posts

This can’t be a problem in qt but I don’t know how to do it properly.

It must be someone else who have done this before.

Maybe I have to use Qhttp instead

December 22, 2011

Volker Volker
Robot Herder
5428 posts

[quote author=“toho71” date=“1324560334”
Maybe I have to use Qhttp instead

You will run into the very same problems with constructing the URL and/or preparing the post data. You will need to understand HTTP protocol basics before using it in a class.

Page  
1

  ‹‹ Sliding stacked widget (too many slots to be called out)      How to suppress warnings in qmake for clang++ ››

You must log in to post a reply. Not a member yet? Register here!