QNetworkReply https API doesn't work on Android devices - QT

541 views Asked by At

I've a problem, I'm make an app with qt, cross platoform, ios & android. So when i call this code:

QList<QString> JsonFunctions::getToken(QString Username, QString Password)
{
    QString lista;
    QEventLoop eventLoop;
    QNetworkAccessManager mgr;
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));

    QJsonObject json;

    json.insert("username", Username);
    json.insert("password", Password);
    json.insert("client_id","10");
    json.insert("client_secret","xxx");
    json.insert("scope","xxx");
    json.insert("grant_type","password");
    json.insert("accept", "application/json");

    QNetworkRequest request(QUrl("https://xxxxx/api/v2/login"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QSslConfiguration conf = request.sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
    request.setSslConfiguration(conf);

    QNetworkReply *reply = mgr.post(request, QJsonDocument(json).toJson());
    eventLoop.exec();

    QString strReply = (QString)reply->readAll();
    qDebug() << "reply" << strReply;
    qDebug() << "code" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    QJsonDocument doc = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject responseObject = doc.object();

    QString token_type = responseObject.value("token_type").toString();
    QString access_token = responseObject.value("access_token").toString();
    QString refresh_token = responseObject.value("refresh_token").toString();

    QList<QString> lists;
    lists.append(token_type);
    lists.append(access_token);
    lists.append(refresh_token);

    return lists;
}

The problem is that on android this code don't work. Instead on ios everything works. the reply on android is empty.

Maybe because the https url create a problem on android call ? I don't understand how to fix it. I've set on the device many permission in the manifest, internet permission is setted.

Now, many idea to works ? I'm testing on asus zen pad 3s and iphone 7. Only on android don't work. Why ? I use qt 5.9

Thanks in advance.

1

There are 1 answers

1
Xplatforms On
  1. QNetworkAccessManager should be used as asynchronous API. When the request is finished finished(QNetworkReply *reply) signal is emitted. Connect to this signal and read data in it.

  2. If you really need to make it "synchronous" connect finished signal to exit() slot of eventLoop. Also better check if there was any SSL errors before.

  3. You will need OpenSSL libs for some Android devices. This was causing troubles for me. OpenSSL/libcrypto.so and OpenSSL/libssl.so was needed. Even if QSslSocket::supportsSsl() returns true.