PDA

View Full Version : [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program



AmirH
19th May 2015, 19:53
Hi,

I'm developping an app on windows which tooltips French news by the system tray.

After succesfully developped the functions that read xml files and fill them, I wanna download the rss file of lemonde.fr (which u know is a xml file) to add the last news in the local xml file.

I tried to do all necessary work for downloading that file but I have a "Unknown error" with reply->errorString(); and then the program crashes.

Here is the principal code :





Window::Window()
{
AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
AppVars::netManList<< new QNetworkAccessManager(this);
QObject::connect (AppVars::netManList[0], SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadedRSSalaune(QNetworkReply*))) ;
for(int i=0;i<AppVars::urlRSSlist.size();i++){
qDebug() << "enter for loop for i = "+QString::number(i);
AppVars::netManList[i]->get(QNetworkRequest(QUrl(AppVars::urlRSSlist[i])));
}

}

void Window::downloadedRSSalaune(QNetworkReply* reply){

qDebug() << reply->errorString();

QByteArray data =QByteArray(reply->readAll());

QList<QString> titleList;
QList<QString> linkList;
QDomDocument *domfile = new QDomDocument("alaune_xml");
if(!(domfile->setContent(data))){
return;
}

QDomNode principalNode = domfile->documentElement();
QDomNode channelNode = principalNode.namedItem("rss").namedItem("channel");
QDomNodeList listItems = channelNode.childNodes();

int k = 0;
int i = 0;
while(k<10 && i<listItems.length()){
if(listItems.at(i).nodeName() == "item"){
qDebug() << listItems.at(i).namedItem("title").toElement().text();
titleList << listItems.at(i).namedItem("title").toElement().text();
linkList << listItems.at(i).namedItem("link").toElement().text();
++k;
}
++i;
}

firstInsert10ArticlesXML(titleList, linkList, "alaune");

tooltipInstantNews("DirectNews France","Les articles sont maintenant à jours.","");
}


void Window::firstInsert10ArticlesXML(QList<QString> titles, QList<QString> links, QString cat)
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);

if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}

// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();

QList<QDomElement *> newItems;
for(int i = 0; i<10; i++){
newItems<<new QDomElement(domfile->createElement("item"));

QDomElement titleSubItem = domfile->createElement("title");
QDomText titleText = domfile->createTextNode(titles[i]);
QDomElement linkSubItem = domfile->createElement("link");
QDomText linkText = domfile->createTextNode(links[i]);

titleSubItem.appendChild(titleText);
linkSubItem.appendChild(linkText);
newItems[i]->appendChild(titleSubItem);
newItems[i]->appendChild(linkSubItem);
}

QDomNode principalNode = domfile->documentElement();

QDomNode catNode = principalNode.namedItem(cat);

if(catNode.isNull())
return;

for(int i = 0; i<10; i++){
catNode.insertBefore(*newItems[i],catNode.firstChild());
}

QString write_doc = domfile->toString();

if(!(AppFiles::historyXML->open(QIODevice::WriteOnly))){
return;
}

AppFiles::historyXML->resize(0);
QTextStream stream(AppFiles::historyXML);
stream << write_doc;
AppFiles::historyXML->close();

ReadXML();
}

void Window::ReadXML()
{
QString filepath = QCoreApplication::applicationDirPath();
filepath.append("\\history.xml");
AppFiles::historyXML->setFileName(filepath);

if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
return;
}

// QDomDocument to parse xml file
QDomDocument *domfile = new QDomDocument("history_xml");
if(!(domfile->setContent(AppFiles::historyXML))){
return;
}
AppFiles::historyXML->close();

QDomNode principalNode = domfile->documentElement();

QList<QDomNode> listNodes;
listNodes<<principalNode.namedItem("alaune")<<principalNode.namedItem("monde")<<principalNode.namedItem("politique")<<principalNode.namedItem("societesante")
<<principalNode.namedItem("economie")<<principalNode.namedItem("science")<<principalNode.namedItem("art")<<principalNode.namedItem("sport")
<<principalNode.namedItem("people");

for(int k=0;k<9;k++){
QDomNode item = listNodes[k].firstChild();
int i = 0;
while(!listNodes[k].isNull() && !item.isNull() && i<10){
//for each article found, enable the QAction, and attribute text and link
listArticles[k][i]->setEnabled(true);
QString titleTrunc = item.namedItem("title").toElement().text();
if(titleTrunc.size() > 50)
titleTrunc = titleTrunc.left(50).append("...");
listArticles[k][i]->setText(titleTrunc);
AppVars::listShortcuts[k][i] = item.namedItem("link").toElement().text();
++i;
item = item.nextSibling();
}
}
}

void Window::tooltipInstantNews(QString title, QString categorie, QString url)
{
// recieved from getrssthread.cpp, show directly (no use of any xml file) the news and attribute system tray message link
sticon->setToolTip(categorie+" : \n"+title);
AppVars::shortcutMessage = url;
if(AppVars::NotifBool)
sticon->showMessage(categorie,title,QSystemTrayIcon::Infor mation);

}


I may have to put some if(!ERROR) but I don't know where and how.

Window class derivates from QWidget
AppVars::urlRSSlist is a QList<String> (global var)
AppVars::netManList is a QList<QNetworkAccessManager*> (global var)

if something not clear, please tell me.

Thank u in advance for your help.

wysota
19th May 2015, 20:51
Show us the debugger stack trace.

AmirH
19th May 2015, 22:14
I really don't remember how to debug.

So I just activated debug mode, put a "red point" just before reply->errorString(); and run as debug.

A lot of things I don't understood appeared and I have a message:
Signal received from Operating system
name SIGSEGV
Meaning: segmentation fault

in a tab below, i have this:
0x66c01df4 8b 30 mov (%eax),%esi

Added after 11 minutes:

how should I check error of reply before QByteArray data =QByteArray(reply->readAll());

I should make a if(reply is null or error or anything else)

Added after 14 minutes:

ok I put :


if(reply->error() != QNetworkReply::NoError)
return;

and it don't seems to return, but I still have "Unkown error" , i don't know why

I will arrange my code so no more segmentation error.

I'll come back if another ptoblem arrive,

still don't understand the "Unknown error"

wysota
19th May 2015, 22:15
I really don't remember how to debug.

Well... I'm sure the web is full of information on how to do that so you can refresh your knowledge.

AmirH
19th May 2015, 22:28
it was just a segmentation fault because of bad use of QList,

but the problem is that i still have qDebug() << reply->errorString(); which return "Unknown error", I have if(reply->error() != QNetworkReply::NoError) which not return (good :) ) and then i put:

qDebug() <<reply->readAll(); which print an empty string ( i see that by "" printed in application output)

but : http://rss.lemonde.fr/c/205/f/3050/index.rss has some result ... so I don't know where to look

AmirH
20th May 2015, 12:53
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ).toInt();
qDebug() << QVariant(statusCode).toString();

prints 200

reply->readAll(); is always empty