PDA

View Full Version : Need help. can't append chars in QString



AcerExtensa
11th June 2008, 16:04
Hi everyone!,

I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
I can't use QHttp class, because it has problem with POST request under wince.
http://trolltech.com/developer/task-tracker/index_html?id=215175&method=entry

here is sample of code:

QString str;
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
&dwNumberOfBytesRead) &&
dwNumberOfBytesRead )
{
str.append(szBuffer);

}

qDebug() << str;


qDebug prints nothing and str.length() == 0, but dwNumberOfBytesRead > 0 :(

Thanks for any help!

lyuts
11th June 2008, 16:13
QString str;
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
&dwNumberOfBytesRead) &&
dwNumberOfBytesRead )
{
str.append(szBuffer);

}

qDebug() << str;



You are not using append correctly. QString's append receives "char*" as a paramete,r but not "char".

AcerExtensa
11th June 2008, 16:32
Compiler says

Converting Parameter 1 from char (*)[256] in QChar inpossible

i can read data by copying each char into string:

for(int i= 0; i<= dwNumberOfBytesRead; i++){
str += QString(szBuffer[i]);
}


but it is not good solution...... :(

jacek
12th June 2008, 00:54
I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
Weird. Maybe the buffer begins with '\0'? What does "qDebug() << QByteArray( szBuffer, dwNumberOfBytesRead )" output?

And you shouldn't disregard dwNumberOfBytesRead, unless you like random crashes.



You are not using append correctly. QString's append receives "char*" as a paramete,r but not "char".
But szBuffer is a char *.

AcerExtensa
12th June 2008, 09:18
Here what I get with qDebug:

"y></SOAP-ENV:Envelope>
"

and here is original response copyed from Wireshark:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:crmlogon" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">dsfsdfdfsdfsdfdsfsdfdsf</return></ns1:crmLoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>


here is the code:


DWORD code = sizeof (DWORD), dwBytesWritten = sizeof (code);
::HttpQueryInfo(hResourceHandle, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &dwBytesWritten, NULL);
qDebug() << "status code:" << code;
DWORD baval = sizeof(DWORD);
::InternetQueryDataAvailable(hResourceHandle, &baval, NULL, NULL);
qDebug() << "Bytes Available:" << baval;

char szBuffer[256];
DWORD dwNumberOfBytesRead = 1;
while(dwNumberOfBytesRead){
if(::InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),&dwNumberOfBytesRead)){
if(dwNumberOfBytesRead == 0) break;
//qDebug() << QByteArray( szBuffer, dwNumberOfBytesRead );
//szBuffer[dwNumberOfBytesRead] = 0;


qDebug() << "BytesRead" << dwNumberOfBytesRead;
str.append(szBuffer);
/*
for(int i= 0; i<= (int)dwNumberOfBytesRead; i++){
str += QString(szBuffer[i]);
}*/
qDebug() << "current str.length()" << str.length();
}
else{ qDebug() << "error:" << GetLastError(); }
}


QFile *file = new QFile("log");
file->open(QIODevice::WriteOnly);
QByteArray bbb;
bbb.append(str);
file->write(bbb);
file->close();

}
else{
qDebug() << "dwErrorCode" << GetLastError();
}
qDebug() << "complete response" << str;
qDebug() << "length" << str.length();


debug output:

status code: 200
Bytes Available: 535
BytesRead 256
current str.length() 258
BytesRead 256
current str.length() 516
BytesRead 23
current str.length() 774
length 774

data in file:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:crmlogon" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC=Hµݢhttp://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">93b262066b4ec01dcafd26f7fb26f53e</return></ns1:crmLoginResponse></SOAP-ENV:BodH޹µ></SOAP-ENV:Envelope>
.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">93b262066b4ec01dcafd26f7fb26f53e</return></ns1:crmLoginResponse></SOAP-ENV:BodH݀µ
µ - on place µ symbol is symbole like that [] filled with black color, don't know wtf is that :)

absolute same result if str is QByteArray.

any idea people?

Conel
12th June 2008, 10:37
Hi everyone!,

I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
I can't use QHttp class, because it has problem with POST request under wince.
http://trolltech.com/developer/task-tracker/index_html?id=215175&method=entry

here is sample of code:

QString str;
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
&dwNumberOfBytesRead) &&
dwNumberOfBytesRead )
{
str.append(szBuffer);

}

qDebug() << str;


qDebug prints nothing and str.length() == 0, but dwNumberOfBytesRead > 0 :(

Thanks for any help!

Problems that I see:

1) array of bytes char* is converted into QString without specifying encoding (which is UTF-8 in that case);
2) since length of data is not provided then char* is read untill first '\0'. In your case it looks like QString::append reads beyond bounds of buffer;
3) why do you need QString here? You are getting array of bytes so keep them into QByteArray;

Try this code:



QString str;
QByteArray ba;
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
&dwNumberOfBytesRead) &&
dwNumberOfBytesRead )
{
QByteArray readData(szBuffer, dwNumberOfBytesRead);
ba.append(readData);
}
str = QString::fromUtf8(ba.data(), ba.length());

qDebug() << str;

AcerExtensa
12th June 2008, 10:57
QByteArray is mutch better for me, I have just played with QBytes and QStrings.

Thanks! I get now data without any extra symbols, but qDebug don't print any, ba.length() prints 535 :)
ok, I don't really need to print this on screen or in console.... lets see if I can parse that data without problem....