How could I fetch data from backend?



  • Could anybody give me some code snippet aims at fetching data from backend? Thanks.
    I try to write some, but seems strange and it does not work.
    @QJsonObject object;
    object["objectType"] = QStringLiteral( "objects.ColorCube_Comment" );
    EnginioReply* reply = m_pClient->query( object );
    ui->textEdit->append( reply->data( )["Comment"].toString( ) );@



  • Hello, ~jiangcaiyang

    Looks like you just need to read those pages of documentation:

    http://qt-project.org/doc/qt-5.0/qtcore/qjsonobject.html
    http://qt-project.org/doc/qt-5.0/qtcore/qjsonvalue.html

    Afair from working with JSON in Qt, line 2 is completely wrong.
    @
    object["objectType"] = QStringLiteral( "objects.ColorCube_Comment" );
    @

    So, here you are trying to set value via QStringLiteral "objects.ColorCube_Comment" - it's not going to be value of json object, it's going to be just QString("objects.ColorCube_Comment").

    Good luck



  • jiangcaiyang sorry for late answer

    The problem is here:
    [code]
    ui->textEdit->append( reply->data( )["Comment"].toString( ) );
    [/code]
    reply->data() will be empty until the reply is finished. The api is asynchronous.

    Solution would be to connect EnginioReply::finished signal to a slot and call
    [code]
    ui->textEdit->append( reply->data( )["Comment"].toString( ) );
    [/code]
    inside it



  • [quote author="Nierob" date="1374763461"]jiangcaiyang sorry for late answer

    The problem is here:
    [code]
    ui->textEdit->append( reply->data( )["Comment"].toString( ) );
    [/code]
    reply->data() will be empty until the reply is finished. The api is asynchronous.

    Solution would be to connect EnginioReply::finished signal to a slot and call
    [code]
    ui->textEdit->append( reply->data( )["Comment"].toString( ) );
    [/code]
    inside it
    [/quote]

    Hi, I've found this solution, by the way, I think the reply Json object may be a little complicated and I have to pick out "result" branch:

    Here is the new version of my code:
    @
    // 1、创建Enginio的客户端类并写出backendID和backendSecret
    m_pClient = new EnginioClient;

        m_pClient->setBackendId( g_BackendID );  
        m_pClient->setBackendSecret( g_BackendSecret );  
      
        // 2、从后台获取数据  
        QJsonObject object;  
        object["objectType"] = QStringLiteral( "objects.ColorCube_Comment" );  
        m_pClient->query( object );  
        connect( m_pClient, SIGNAL( finished( EnginioReply* ) ),  
                 this, SLOT( DownloadText( EnginioReply* ) ) );  
                  
    void FeedbackDialog::DownloadText( EnginioReply* pReply )  
    {  
        // 3、解析从后端获取的数据  
        QJsonArray result = pReply->data( )["results"].toArray( );  
      
        foreach ( const QJsonValue& value, result )  
        {  
            const QJsonObject& object = value.toObject( );  
            const QString& time = object["updatedAt"].toString( );  
            const QString& comment = object["Comment"].toString( );  
            ui->textEdit->append( time + ": " + comment );  
        }  
      
        ……  
    }  
    

    @

    Also I've written a blog about how to use Enginio:
    http://blog.csdn.net/jiangcaiyang123/article/details/9465757


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.