How can I get things from website
-
@victor-wang You just need to open the docs to find the solution: http://doc.qt.io/qt-5/qnetworkreply.html#error-1 instead of
&QNetworkReply::error
usestatic_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error)
orQOverload<(QNetworkReply::NetworkError)>::(&QNetworkReply::error)>
-
@victor-wang
My bad, QNetworkReply::error is an overloaded function.use this:
connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ qDebug() << code; });
taken from the docu
-
@J.Hilk said in How can I get things from website:
connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ qDebug() << code; });
taken from the docu
In Qt 5.7 onwards, you can also use
qOverload()
(with C++14) orQOverload
(C++11), instead of the longer static_cast: http://doc.qt.io/qt-5/qtglobal.html#qOverloadconnect(networkReply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code) { qDebug() << code; });
-
-
@victor-wang said in How can I get things from website:
And this is the error that i got here
You have a warning message: "lambda expressions only available with -std=c++11 or -std=gnu++11"
That means haven't enabled C++11.
Add this line to your *.pro file to enable it:
CONFIG += C++11
-
@JKSH
I can compiled it successfully.
But i don't know what this is doing ?
I still can't get any information from my Url.This is my code here
And this is what i got here
I didn't get any error back but also didn't get what i want back either.
This is the Url i want to get http://192.168.100.59/PLMS/TestCheck.asp?LSN=201503003009&EmpId=we9999&CPID=OS&CPKey=95Z0000000AF
I should get a file or get a respond that said ok.
-
Please post your code as text, not pictures. Text is much easier to read and search.
Anyway, what is inside your
replyFinished()
function? -
void MainWindow::replyFinished(QNetworkReply *reply) { qDebug("connect content"); QTextCodec *codec=QTextCodec::codecForLocale(); int connect_url=reply -> attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<connect_url; qDebug("here"); }
Sorry, i'm using Qt via Xming and i can't just copy the code to my local computer.
I have to type myself.For the picture i just gave you.
"connect_url" will print the number 200 out to make sure the Url is connecting successfully. -
@victor-wang said in How can I get things from website:
"connect_url" will print the number 200 out to make sure the Url is connecting successfully.
Yes, 200 means "HTTP OK": http://www.restapitutorial.com/httpstatuscodes.html
Your code is connecting to the server OK.
void MainWindow::replyFinished(QNetworkReply *reply) { qDebug("connect content"); QTextCodec *codec=QTextCodec::codecForLocale(); int connect_url=reply -> attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<connect_url; qDebug("here"); }
You need to read the data from the QNetworkReply. Call
reply->readAll()
to get the data.Also, remember to delete your QNetworkReply after you have finished, or else you will have a memory leak. See the QNetworkReply documentation: http://doc.qt.io/qt-5/qnetworkreply.html
@victor-wang said in [How can I get things from website](/topic/77075/how-can-i-get-things-from-
I should get a file
As I said above, you must call
reply->readAll()
to get the data. Then, write the data into a file using a QFile. -
@victor-wang said in How can I get things from website:
@JKSH
you mean i have to add this after the connect function?qDebug()<<reply->readAll(); reply->deleteLater();
I mean you have to add this inside your
replyFinished()
slot.Are you familiar with asynchronous programming? Networking is asynchronous. When you call
QNetworkAccessManager::get()
, it starts downloading. However, it doesn't wait for the download to finish. Instead, QNetworkAccessManager and QNetworkReply WILL emit thefinished()
signal when the download finishes. So, you can read the downloaded data from inside the connected slot.Is that readAll will print the information out?
Or i have to using qDebug to print it out?readAll() gives you the data in a QByteArray. It doesn't print anything. Do you know how to use QByteArray?
qDebug prints to the console.
-
I am not quite sure that i'm using the right way.
I write this code into replyFinished();QByteArray output; output = reply -> readAll(); qDebug()<<output; reply->deleteLater();
Am i right?
Sorry i'm new in Qt please be patient to me:( -
@victor-wang said in How can I get things from website:
I write this code into replyFinished();
QByteArray output; output = reply -> readAll(); qDebug()<<output; reply->deleteLater();
Am i right?
Yes, that is right :)
Now, you can save
output
into a file, using QFile.Sorry i'm new in Qt please be patient to me:(
No problem! We are happy to help beginners. I think you are learning well.
-
I add this after connect instead of replyFinished slot.
file.setFileName("/home/check.txt"); if(!file.exist)qDebug("not exist"); if(!file.open(QIODevice::WriteOny | QIODevice::Text))qDebug("not write success"); stream.setDevice(&file); stream<<out; file.close;
But when i went to cat the check.txt there is nothing in the file.
And i add this in my replyFinished slot.
out=reply->readAll(); qDebug()<<out; reply->deleteLater();
The "QByteArray out" i had declared it in the mainwidow.h for global type.
Did i need to put in the private place or the public place in the mainwindow.h?Why i still didn't see anything in my file?
-
@victor-wang said in How can I get things from website:
I this function will show the content out?
qDebug()<<output;
Yes.
The "QByteArray out" i had declared it in the mainwidow.h for global type.
Did i need to put in the private place or the public place in the mainwindow.h?No, no need.
@victor-wang said in How can I get things from website:
I add this after connect instead of replyFinished slot.
That definitely won't work, because you haven't received the data yet.
Remember, networking is asynchronous. You can only read the data after the
finished()
signal is emitted.If it is why it showed NULL on the console. here
I thought it will print the Url information which i had just gave you "Updated ok!".
...
Why i still didn't see anything in my file?This is where you need to start debugging and testing.
- What do you get when you type your URL into your browser?
- What happens when you change the URL to http://lists.qt-project.org/mailman/listinfo ?
- Is your QNetworkRequest constructed correctly?
- Is your device on the same subnet as the server?
-
@JKSH
Is that you mean i have to add this QFile function in the replyFinisheed() slot?
Or i have to do it when the finished signal is up?
If it is how can i judge the signal and where do i have to add the function in?For the first of debugging and testing.
I will get this on my browser. hereFor the second one, i'm not quite sure what does that mean.
For third one, what does"QNetworkRequest constructly correctly"mean?
I thought i am using the library that Qt have already constructed.For forth one, I'm sure i am on the same subnet because i can send this commend to my console
wget http://192.168.100.59/PLMS/TestCheck.asp?LSN=201503003009&EmpId=we9999&CPID=OS&CPKey=95Z0000000AF
And i will get the file and the content will show "updated ok".
-
@JKSH
I had add qDebug() function in replyFinished() slot.
Here is my code.According to what you said it will print something out if i am in the same subnet.
And i'm sure that i am in the same subnet but why it still print NULL out like this?According to the picture i gave you, i assume that it will return "Updated ok!" back to me.
-
@victor-wang said in How can I get things from website:
Or i have to do it when the finished signal is up?
@JKSH already said several times that you have to do it in the slot connected to the finished() signal. So, yes do it in when you get finished() signal.
To your code: you call readAll twice - that is probably the reason why qDebug() << out; prints nothing. You should call readAll only once and then print the data.