Results 1 to 6 of 6

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

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Post [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    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 :

    Qt Code:
    1. Window::Window()
    2. {
    3. AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
    4. AppVars::netManList<< new QNetworkAccessManager(this);
    5. QObject::connect (AppVars::netManList[0], SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadedRSSalaune(QNetworkReply*))) ;
    6. for(int i=0;i<AppVars::urlRSSlist.size();i++){
    7. qDebug() << "enter for loop for i = "+QString::number(i);
    8. AppVars::netManList[i]->get(QNetworkRequest(QUrl(AppVars::urlRSSlist[i])));
    9. }
    10.  
    11. }
    12.  
    13. void Window::downloadedRSSalaune(QNetworkReply* reply){
    14.  
    15. qDebug() << reply->errorString();
    16.  
    17. QByteArray data =QByteArray(reply->readAll());
    18.  
    19. QList<QString> titleList;
    20. QList<QString> linkList;
    21. QDomDocument *domfile = new QDomDocument("alaune_xml");
    22. if(!(domfile->setContent(data))){
    23. return;
    24. }
    25.  
    26. QDomNode principalNode = domfile->documentElement();
    27. QDomNode channelNode = principalNode.namedItem("rss").namedItem("channel");
    28. QDomNodeList listItems = channelNode.childNodes();
    29.  
    30. int k = 0;
    31. int i = 0;
    32. while(k<10 && i<listItems.length()){
    33. if(listItems.at(i).nodeName() == "item"){
    34. qDebug() << listItems.at(i).namedItem("title").toElement().text();
    35. titleList << listItems.at(i).namedItem("title").toElement().text();
    36. linkList << listItems.at(i).namedItem("link").toElement().text();
    37. ++k;
    38. }
    39. ++i;
    40. }
    41.  
    42. firstInsert10ArticlesXML(titleList, linkList, "alaune");
    43.  
    44. tooltipInstantNews("DirectNews France","Les articles sont maintenant à jours.","");
    45. }
    46.  
    47.  
    48. void Window::firstInsert10ArticlesXML(QList<QString> titles, QList<QString> links, QString cat)
    49. {
    50. QString filepath = QCoreApplication::applicationDirPath();
    51. filepath.append("\\history.xml");
    52. AppFiles::historyXML->setFileName(filepath);
    53.  
    54. if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
    55. return;
    56. }
    57.  
    58. // QDomDocument to parse xml file
    59. QDomDocument *domfile = new QDomDocument("history_xml");
    60. if(!(domfile->setContent(AppFiles::historyXML))){
    61. return;
    62. }
    63. AppFiles::historyXML->close();
    64.  
    65. QList<QDomElement *> newItems;
    66. for(int i = 0; i<10; i++){
    67. newItems<<new QDomElement(domfile->createElement("item"));
    68.  
    69. QDomElement titleSubItem = domfile->createElement("title");
    70. QDomText titleText = domfile->createTextNode(titles[i]);
    71. QDomElement linkSubItem = domfile->createElement("link");
    72. QDomText linkText = domfile->createTextNode(links[i]);
    73.  
    74. titleSubItem.appendChild(titleText);
    75. linkSubItem.appendChild(linkText);
    76. newItems[i]->appendChild(titleSubItem);
    77. newItems[i]->appendChild(linkSubItem);
    78. }
    79.  
    80. QDomNode principalNode = domfile->documentElement();
    81.  
    82. QDomNode catNode = principalNode.namedItem(cat);
    83.  
    84. if(catNode.isNull())
    85. return;
    86.  
    87. for(int i = 0; i<10; i++){
    88. catNode.insertBefore(*newItems[i],catNode.firstChild());
    89. }
    90.  
    91. QString write_doc = domfile->toString();
    92.  
    93. if(!(AppFiles::historyXML->open(QIODevice::WriteOnly))){
    94. return;
    95. }
    96.  
    97. AppFiles::historyXML->resize(0);
    98. QTextStream stream(AppFiles::historyXML);
    99. stream << write_doc;
    100. AppFiles::historyXML->close();
    101.  
    102. ReadXML();
    103. }
    104.  
    105. void Window::ReadXML()
    106. {
    107. QString filepath = QCoreApplication::applicationDirPath();
    108. filepath.append("\\history.xml");
    109. AppFiles::historyXML->setFileName(filepath);
    110.  
    111. if(!(AppFiles::historyXML->open(QIODevice::ReadOnly))){
    112. return;
    113. }
    114.  
    115. // QDomDocument to parse xml file
    116. QDomDocument *domfile = new QDomDocument("history_xml");
    117. if(!(domfile->setContent(AppFiles::historyXML))){
    118. return;
    119. }
    120. AppFiles::historyXML->close();
    121.  
    122. QDomNode principalNode = domfile->documentElement();
    123.  
    124. QList<QDomNode> listNodes;
    125. listNodes<<principalNode.namedItem("alaune")<<principalNode.namedItem("monde")<<principalNode.namedItem("politique")<<principalNode.namedItem("societesante")
    126. <<principalNode.namedItem("economie")<<principalNode.namedItem("science")<<principalNode.namedItem("art")<<principalNode.namedItem("sport")
    127. <<principalNode.namedItem("people");
    128.  
    129. for(int k=0;k<9;k++){
    130. QDomNode item = listNodes[k].firstChild();
    131. int i = 0;
    132. while(!listNodes[k].isNull() && !item.isNull() && i<10){
    133. //for each article found, enable the QAction, and attribute text and link
    134. listArticles[k][i]->setEnabled(true);
    135. QString titleTrunc = item.namedItem("title").toElement().text();
    136. if(titleTrunc.size() > 50)
    137. titleTrunc = titleTrunc.left(50).append("...");
    138. listArticles[k][i]->setText(titleTrunc);
    139. AppVars::listShortcuts[k][i] = item.namedItem("link").toElement().text();
    140. ++i;
    141. item = item.nextSibling();
    142. }
    143. }
    144. }
    145.  
    146. void Window::tooltipInstantNews(QString title, QString categorie, QString url)
    147. {
    148. // recieved from getrssthread.cpp, show directly (no use of any xml file) the news and attribute system tray message link
    149. sticon->setToolTip(categorie+" : \n"+title);
    150. AppVars::shortcutMessage = url;
    151. if(AppVars::NotifBool)
    152. sticon->showMessage(categorie,title,QSystemTrayIcon::Information);
    153.  
    154. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    Show us the debugger stack trace.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    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 :

    Qt Code:
    1. if(reply->error() != QNetworkReply::NoError)
    2. return;
    To copy to clipboard, switch view to plain text mode 

    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"
    Last edited by AmirH; 19th May 2015 at 21:14.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    Quote Originally Posted by AmirH View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    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

  6. #6
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [QNetworkAccessManager] Download a file -> "Unknown error" + crash of program

    Qt Code:
    1. int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    2. qDebug() << QVariant(statusCode).toString();
    To copy to clipboard, switch view to plain text mode 

    prints 200

    reply->readAll(); is always empty

Similar Threads

  1. Replies: 1
    Last Post: 2nd September 2014, 09:19
  2. QNetworkReply and "unknown error" problem
    By hybrid_snyper in forum Newbie
    Replies: 3
    Last Post: 3rd December 2012, 00:45
  3. Replies: 4
    Last Post: 17th September 2012, 15:23
  4. "Cannot run program "C:\Qt\4.3.3\bin\qmake": file not found
    By PeteH in forum Installation and Deployment
    Replies: 1
    Last Post: 7th February 2009, 00:48
  5. "unknown test function error" with MAc
    By tommy in forum Installation and Deployment
    Replies: 2
    Last Post: 22nd August 2008, 18:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.