[gelöst] QHttp

Alles rund um die Programmierung mit Qt
Antworten
webcd
Beiträge: 5
Registriert: 24. April 2006 03:45
Kontaktdaten:

[gelöst] QHttp

Beitrag von webcd »

Leider ist hier irgendwo ein wurm drinnen, sodass dass programm nichts downloadet :cry:
ich bekomme regelmäßig 3 Fehlermeldungen:
1. setHost
2. setUser
3. get
nur kann ja nicht alles schiefgehen

hier die Klasse, die in meinem Prog QHTTP verwendet

Code: Alles auswählen

// ****************Splash******************** \\

splashup::splashup(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    QPixmap splashbmp(":/splash.png");
    QLabel *splashimg = new QLabel(this);
    splashimg->setBackgroundRole(QPalette::Base);
    splashimg->setScaledContents(true);
    splashimg->setPixmap(splashbmp);
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(splashimg);
    layout->setMargin(0);
    layout->setSpacing(0);
    setLayout(layout);
    url.setUrl("http://www.webcd.eu/produkte2.xml");
    fileInfo.setFile(url.path());
    fileName = "/tmp/"+fileInfo.fileName();
    httpobj = new QHttp;
    httpobj->setHost(url.host(), url.port() != -1 ? url.port() : 80);
    if (!url.userName().isEmpty())
        httpobj->setUser(url.userName(), url.password());

    connect(httpobj, SIGNAL(requestFinished(int, bool)),
            this, SLOT(httpRequestFinished(int, bool)));
    connect(httpobj, SIGNAL(dataReadProgress(int, int)),
            this, SLOT(updateDataReadProgress(int, int)));
    connect(httpobj, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
            this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    connect(httpobj, SIGNAL(done (bool)),
            this, SLOT(done(bool)));
    connect(httpobj, SIGNAL(stateChanged(int)),
            this, SLOT(stateChanged(int)));
    progress = new QProgressBar;
    layout->addWidget(progress);
    progress->setOrientation(Qt::Horizontal);
    progress->setTextVisible(true);
    status = new QLabel;
    status->setText (tr("k.a."));
    layout->addWidget(status);
    isready=true;
    mainwindow = new amway;
    mainwindow->setFixedSize(900, 720);

        file = new QFile(fileName);
        if (!file->open(QIODevice::WriteOnly)) {
            QMessageBox::information(this, tr("File Open"), tr("An Error occurred."));
            delete file;
            file = 0;
            return;
        }
}

void splashup::download()
{
   httpobj->get(url.path(), file);
}

void splashup::httpRequestFinished(int requestId, bool error)
{
    if(!error)
    {
        QMessageBox::information(this, tr("HTTP"), tr("An Error occurred."));
    }
}

void splashup::updateDataReadProgress(int bytesRead, int totalBytes)
{
    progress->setRange(0,totalBytes);
    progress->setValue(bytesRead);
}

void splashup::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
    
}

void splashup::readResponseHeader(bool error)
{
    if(!error)
    {
        QMessageBox::information(this, tr("HTTP2"), tr("An Error occurred."));
    }
    else
    {
        
    }
}

void splashup::done(bool error)
{
    if(!error)
    {
        QMessageBox::information(this, tr("HTTP"), tr("An Error occurred."));
    }
    else
    {
        //file->close();
        isready=false;
        hide();
        mainwindow->show();
    }
}

void splashup::stateChanged(int statushttp)
{
    if(statushttp=0)
    {
        status->setText (tr("Unconnected"));
    }
    if(statushttp=1)
    {
        status->setText (tr("Host Lookup"));
    }
    if(statushttp=2)
    {
        status->setText (tr("Connecting"));
    }
    if(statushttp=3)
    {
        status->setText (tr("Sending"));
    }
    if(statushttp=4)
    {
        status->setText (tr("Reading"));
    }
    if(statushttp=5)
    {
        status->setText (tr("Connected"));
    }
    if(statushttp=6)
    {
        status->setText (tr("Closing"));
    }
}

bool splashup::ready()
{
    return isready;
}
Zuletzt geändert von webcd am 24. April 2006 17:03, insgesamt 1-mal geändert.
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

...
if (!error)
OutputErrorMesage()

Irgendwie versteh ich das nicht...
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
goofy
Beiträge: 38
Registriert: 4. April 2006 15:40

Beitrag von goofy »

Hallo,

also in der ersten readResponseHeader Methode, solltest du dir den Status des Headers ausgeben lassen.

Code: Alles auswählen

if(header.statusCode() == 200)
{
    cout << "response ok ..." << endl;      
}
else cout << "bad response ... " << header.statusCode() << endl;
Dann siehst du schon mal ob es überhaupt funktioniert hat.

Es ist auch besser in httpRequestFinished(int requestId, bool error)
das File zuschließen, dann weißt du sicher das alle Daten da sind.

hoffe das hilft ...

goofy


p.s wird es compiliert? weil httpobj = new QHttp;? Besser wäre httpobj = new QHttp();
webcd
Beiträge: 5
Registriert: 24. April 2006 03:45
Kontaktdaten:

Beitrag von webcd »

Hallo,

also in der ersten readResponseHeader Methode, solltest du dir den Status des Headers ausgeben lassen.

Code:
if(header.statusCode() == 200)
{
cout << "response ok ..." << endl;
}
else cout << "bad response ... " << header.statusCode() << endl;


Dann siehst du schon mal ob es überhaupt funktioniert hat.

Es ist auch besser in httpRequestFinished(int requestId, bool error)
das File zuschließen, dann weißt du sicher das alle Daten da sind.

hoffe das hilft ...

goofy
ja :D es hat sehr geholfen, aber ich schließe die Datei schon beim signal HTTP::done()

Code: Alles auswählen

void splashup::done(bool error)
{
    if(error)
    {
        QMessageBox::information(this, tr("HTTP"), tr("An Error occurred."));
    }
    else
    {
        file->close();
        isready=false;
        hide();
        mainwindow->show();
    }
}
p.s wird es compiliert? weil httpobj = new QHttp;? Besser wäre httpobj = new QHttp();
Ja, aber so ist es natürlich korrekt. :D
Antworten