I've got a server, which ought to send image's QByteArray toBase64 format (attached in xml) to the client. Server itself works fine when I Run it, but when started Degubbing instead of just Running. The server doesn't send a image xml with appended QByteArray. I can not understand what is the reason for that? It shows no error while server started by Debugging, by the way. The image is on the server side and its server task to send an image. Here is how server extracts the image by method getVCardImage:
QByteArray &VCardExtensionPrivate::getVCardImage(const QString &imageName,
const QString& type)
{
if(!m_image.isNull()) m_image.clear();
QString imagePath = QString("%1/avatars/%2").arg(QCoreApplication::applicationDirPath())
.arg(imageName);
QImage image;
image.load(imagePath);
if(!image.isNull()) {
QBuffer buffer(&m_image);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, imageType(type.toUpper()));
}
return m_image;
}
And I do save QByteArray into xml:
QDomElement newNodeTag = doc.createElement(QString("BINVAL"));
QDomText newNodeText = doc.createTextNode(getVCardImage(m_imagePath, photoType).toBase64());
Sending through
server()->sendPacket(xml)` \\ xml is QDomElement with <BINVAL>
Start Run works fine!
<BINVAL>`QByteArray(toBase64) format`<\BINVAL>
Start Debugging
<BINVAL>`empty without any QByteArray`<\BINVAL>
Related
I am trying to clone the audio streaming model of QTCpsocket but now using QUdpsocket (virtual connection), though it looks like the code is being executed , nevertheless, effectively its not doing the job, I cant get streamed audio captured;
Main focus point is: is it possible to start a QAudioOutput with a QUDpsocket ???
Yet to clearly mention that this code works fine with a TCP socket!
Code snippet:
in server.h file
private:
QUdpSocket *socketUDP;
and in .CPP file
udpServer::udpServer(QObject *parent) : QObject(parent)
{
socketUDP = new QUdpSocket(this);
serverAddress = QHostAddress("192.168.1.8");
//socketUDP->bind(serverAddress, 1357);
socketUDP->bind(1357, QUdpSocket::ShareAddress);
socketUDP->open(QIODevice::ReadOnly);
connect(socketUDP, &QUdpSocket::readyRead, this, &udpServer::playStreamedAudio);
}
then the playstream() method:
void udpServer::playStreamedAudio() {
// set the QAudioFormat parameters of output audio device.
my_QAudioFormat = new QAudioFormat;
my_QAudioFormat->setSampleRate(48000);
my_QAudioFormat->setChannelCount(1);
my_QAudioFormat->setSampleSize(8);
my_QAudioFormat->setCodec("audio/pcm");
my_QAudioFormat->setByteOrder(QAudioFormat::LittleEndian);
my_QAudioFormat->setSampleType(QAudioFormat::UnSignedInt);
//
// get default audio output device
audiOutputDevice = QAudioDeviceInfo::defaultOutputDevice();
audiooutput = new QAudioOutput(audiOutputDevice,my_QAudioFormat, this);
// attach to socket!
qDebug() << "Playaing AudioStream";
socketUDP->open(QIODevice::ReadOnly);
audiooutput->start(socketUDP); // the Audio output device shall listen to server socket for audio
}
It turns out that UDP sockets may not suitability be interfaced as QioDevices... seems they are not intended to be, the packets are better be written to a file and then processed.
I am trying to send a file via xmlrpc as a base64 encoded string.
QFile sourceFile("/home/alexander/test.jpg");
QByteArray srcRawData = sourceFile.readAll();
QByteArray base64Encoded = srcRawData.toBase64();
std::string strTest(base64Encoded);
xmlrpc_c::paramList sampleAddParms;
sampleAddParms.add(xmlrpc_c::value_string("113957571"));
sampleAddParms.add(xmlrpc_c::value_string(strTest));
sampleAddParms.add(xmlrpc_c::value_string("test.jpg"));
When I send this to my server I get:
Client threw error: RPC response indicates failure. Bad parameter type (ap_adddocument): expected base64 for parameter data, got (Python) .
This is the class definition o xmlrpc_c::value_*
http://xmlrpcpp.sourceforge.net/doc/classXmlRpc_1_1XmlRpcValue.html
I just realized I need to use the xmlrpc_c::value_bytestring() instead. How do I convert a File into a bytestring in C++? I have a ByteArray already.
I am at a loss here..
Looking at your code, it seems that you can use xmlrpc_c::value_string()
Convert your base64 string in to a const char* and try. Following code should work. But still enough detail is not available in your question.
QFile sourceFile("/home/alexander/test.jpg");
QByteArray base64Encoded = sourceFile.readAll().toBase64();
const char* b64Enc = QString::fromLatin1(base64Encoded).toStdString().c_str();
xmlrpc_c::paramList sampleAddParms;
sampleAddParms.add(xmlrpc_c::value_string("113957571"));
sampleAddParms.add(xmlrpc_c::value_string(b64Enc));
sampleAddParms.add(xmlrpc_c::value_string("test.jpg"));
I am trying to develop file uploader using Qt. Here is my code :
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkRequest request(url);
QByteArray line;
QFile file(//path);
while(!file.atEnd())
{
line.append(file.readLine());
}
file.close();
QObject::connect(manager,SIGNAL(finished(QNetworkReply *)), this, SLOT(error_On_File_Send(QNetworkReply *)));
manager->post(request, line);
it works ok for small files. But it is not working in large file and gives std:bad_alloc error. What should i do
Use QNetworkAccessManager::post ( const QNetworkRequest & request, QIODevice * data ). It will automaticly read data when it is needed from any QIODevice. QFile is QIODevice so you should only slightly change your code. Note that you have to manage attached QIODevice by yourself (delete it after finished() signal)
Here's the situation - I attach some file (JPEG image) to the HTTP POST request so it should be sent to the local server via Wi-Fi and server should return some result (simple text).
Here's the problem I faced doing this in my Qt-application for Nokia N9 (Meego 1.2 Harmattan).
After request is sent, proceed by the server and answer is sent back (I can see log on the server) there's a huge delay (about 1 min) before data from server reaches the handset.
If answer returns in several parts - the delay is before first part and others are getting very fast (as it should be with the first one too).
The same code I use in the same app for Symbian^3 (Symbian Anna) on Nokia C6-01 and it works just fine - all the data returns in a couple of seconds (tested in the same network with the same server and request). Also I have several GET requests sending from this app to the same server and all of them works fine too. So it might be the only Meego problem.
Snippets:
void PostDownloader::sendPostJpgImage(QString url, QImage image) {
if(mainReply)
return;
char boundary[] = "AyV04a234DsHeKHcvNds";
image = image.convertToFormat(QImage::Format_RGB888);
QByteArray body;
QBuffer buffer(&body);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPG");
buffer.close();
QByteArray b;
b.append("--").append(boundary).append("\r\n");
b.append("Content-Disposition: form-data; name=\"jpgfile\"; filename=\"camera\"\r\n");
b.append("Content-Type: image/jpeg\r\n");
b.append("\r\n");
b.append(body);
b.append("\r\n");
b.append("--").append(boundary).append("--");
QNetworkRequest req = QNetworkRequest(QUrl(url));
req.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(QString("multipart/form-data; boundary=")+boundary));
req.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(b.size()));
req.setRawHeader("Connection", "Close");
req.setRawHeader("Cache-Control", "no-cache");
req.setRawHeader("Keep-Alive", "1");
mainReply = manager->post(req, b); //POST
connect(mainReply, SIGNAL(readyRead()), this, SLOT(dataReceived()));
connect(mainReply, SIGNAL(finished()), this, SLOT(finished()));
}
So the delay is before calling the dataReceived() slot. How can this can be solved? What can you advice?
Thanks in advance.
I am trying to connect my application with a web service and here ,a user suggested to send custom headers back to my application.
I am using this code
void Coonnec::serviceRequestFinished(QNetworkReply *reply)
{
QByteArray bytes = reply->readAll();
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Reply error: " + reply->errorString();
}
else
{
qDebug() << "Uploaded: " + QDateTime::currentDateTime().toString();
qDebug() << reply->rawHeaderList();
}
reply->close();
bytes.clear();
reply->deleteLater();
}
from php i send this header
header('XAppRequest-Status: complete');
When running the application i can see that i get this header but i can't take the value of it cause
reply->rawHeader(bytes);
returns nothing.
How can i take the value 'complete'?
I suggest to connect a slot to the void QNetworkReply::metaDataChanged () signal of your reply.
The Qt doc says
This signal is emitted whenever the metadata in this reply changes.
metadata is any information that is not the content (data) itself,
including the network headers. In the majority of cases, the metadata
will be known fully by the time the first byte of data is received.
However, it is possible to receive updates of headers or other
metadata during the processing of the data.
I do use web-services/client with Qt and I noticed that some header's information are not available when I expected it to be ! I had to 'wait' for this signal to check the header content.