forked from lubuwest/Friendiqa
Version 0.001
This commit is contained in:
parent
1986e59b58
commit
2bc729d02c
77 changed files with 6020 additions and 819 deletions
168
v0.001/source-android/common/xhr.cpp
Normal file
168
v0.001/source-android/common/xhr.cpp
Normal file
|
@ -0,0 +1,168 @@
|
|||
#include "xhr.h"
|
||||
|
||||
#include <QHttpPart>
|
||||
#include <QTextCodec>
|
||||
#include <QUrlQuery>
|
||||
|
||||
#include "uploadableimage.h"
|
||||
|
||||
XHR *XHR::instance()
|
||||
{
|
||||
static XHR xhr;
|
||||
return &xhr;
|
||||
}
|
||||
|
||||
XHR::XHR(QObject *parent) : QObject(parent)
|
||||
{
|
||||
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
|
||||
}
|
||||
|
||||
void XHR::setUrl(QString url)
|
||||
{
|
||||
if (url!=m_url) {
|
||||
m_url = url;
|
||||
emit urlChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void XHR::setLogin(QString login)
|
||||
{
|
||||
if (login!=m_login) {
|
||||
m_login = login;
|
||||
emit loginChanged();
|
||||
}
|
||||
}
|
||||
QString XHR::url() const
|
||||
{
|
||||
return m_url;
|
||||
}
|
||||
|
||||
QString XHR::login() const
|
||||
{
|
||||
return m_login;
|
||||
}
|
||||
|
||||
void XHR::setParam(QString name, QString value)
|
||||
{
|
||||
params.insert(name, value);
|
||||
}
|
||||
|
||||
void XHR::setImageFileParam(QString name, QString url)
|
||||
{
|
||||
files.insert(name, url);
|
||||
}
|
||||
|
||||
void XHR::clearParams()
|
||||
{
|
||||
files.clear();
|
||||
params.clear();
|
||||
}
|
||||
|
||||
void XHR::get()
|
||||
{
|
||||
QUrlQuery query;
|
||||
|
||||
QHashIterator<QString, QString> i(params);
|
||||
while(i.hasNext()) {
|
||||
i.next();
|
||||
query.addQueryItem(i.key(), i.value());
|
||||
}
|
||||
|
||||
QUrl requrl(m_url);
|
||||
requrl.setQuery(query);
|
||||
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
|
||||
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, &XHR::onReplySuccess);
|
||||
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onReplyError(QNetworkReply::NetworkError)));
|
||||
connect(reply, &QNetworkReply::readyRead, this, &XHR::onReadyRead);
|
||||
|
||||
}
|
||||
|
||||
void XHR::post()
|
||||
{
|
||||
qDebug() << "start post to " << m_url;
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
|
||||
QHashIterator<QString, QString> iparams(params);
|
||||
while(iparams.hasNext()) {
|
||||
iparams.next();
|
||||
qDebug() << "\t add param " << iparams.key() << " : " << iparams.value();
|
||||
QHttpPart textPart;
|
||||
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"" + iparams.key() + "\""));
|
||||
|
||||
|
||||
textPart.setBody(iparams.value().toUtf8());
|
||||
multiPart->append(textPart);
|
||||
}
|
||||
|
||||
UploadableImage uimg;
|
||||
QHashIterator<QString, QString> ifiles(files);
|
||||
while(ifiles.hasNext()) {
|
||||
ifiles.next();
|
||||
|
||||
uimg.setSource(ifiles.value());
|
||||
qDebug() << "\t image: " << uimg.mimetype() << ", " << ifiles.key();
|
||||
|
||||
QHttpPart imagePart;
|
||||
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(uimg.mimetype()));
|
||||
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"" + ifiles.key() + "\"; filename=\""+uimg.filename()+"\""));
|
||||
imagePart.setBody(uimg.bytes());
|
||||
multiPart->append(imagePart);
|
||||
}
|
||||
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader(QByteArray("Authorization"), headerData.toLocal8Bit());
|
||||
|
||||
request.setUrl(m_url);
|
||||
reply = manager.post(request, multiPart);
|
||||
qDebug() << "\t request sent";
|
||||
connect(reply, &QNetworkReply::finished, this, &XHR::onReplySuccess);
|
||||
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onReplyError(QNetworkReply::NetworkError)));
|
||||
connect(reply, &QNetworkReply::readyRead, this, &XHR::onReadyRead);
|
||||
connect(reply, &QNetworkReply::sslErrors, this, &XHR::onSSLError);
|
||||
qDebug() << "\t reply signals connected";
|
||||
}
|
||||
|
||||
void XHR::onReplyError(QNetworkReply::NetworkError code)
|
||||
{
|
||||
qDebug() << code;
|
||||
emit this->error( bufferToString(), (int) code);
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void XHR::onReplySuccess()
|
||||
{
|
||||
qDebug() << "!";
|
||||
emit this->success( bufferToString() );
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void XHR::onReadyRead()
|
||||
{
|
||||
qDebug() << ".";
|
||||
buffer += reply->readAll();
|
||||
}
|
||||
|
||||
void XHR::onSSLError(const QList<QSslError> &errors)
|
||||
{
|
||||
qDebug() << "XHR::onSSLError :" ;
|
||||
QListIterator<QSslError> ierrs(errors);
|
||||
while(ierrs.hasNext()) {
|
||||
qDebug() << "\t" << ierrs.next().errorString();
|
||||
}
|
||||
}
|
||||
|
||||
QString XHR::bufferToString()
|
||||
{
|
||||
return QTextCodec::codecForName("utf-8")->toUnicode(buffer);
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue