Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: Problems parsing json from qbytearray

Alternate ways to do the same thing...

 

Properties, and QML is bound to them,

OR

QML handles parsing the JSON and sets the data to display on the controls.

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: Problems parsing json from qbytearray

Thank you, how I made the bound to the country property on qml?
Please use plain text.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: Problems parsing json from qbytearray

I did it!!! Thanks a lot! :smileyvery-happy:
Please use plain text.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: Problems parsing json from qbytearray

hi, i have a problem yesterday, i got country working but after i finish with the other properties and everything, just the country works, i dont know what went wrong

 

This is the cpp file

 

  QString response;
    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            const int available = reply->bytesAvailable();
            if (available > 0) {
                const QByteArray buffer(reply->readAll());

                bb::data::JsonDataAccess ja;
                QVariantMap map = (ja.loadFromBuffer(buffer).toMap());
                setName(map["name"].toString());
                QVariantMap sys (map["sys"].toMap());
                setCountry(sys["country"].toString());
                QVariantList weather (map["weather"].toList());
                QVariantMap weather0(weather[0].toMap());
                setDescription(weather0["description"].toString());
                QVariantMap main (map["main"].toMap());
                setTemp(main["temp"].toString());
                setHumidity(main["humidity"].toString());
                setTemp_min(main["temp_min"].toString());
                setTemp_max(main["temp_max"].toString());
                QVariantMap wind (map["wind"].toMap());
                setSpeed(wind["speed"].toString());
                setDeg(wind["deg"].toString());
            }
        } else {
            // Get http status code
            const int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

            // Do some error management
            response = tr("Http Error: %1").arg(httpStatus);
        }

        reply->deleteLater();
    }

    if (response.trimmed().isEmpty()) {
        response = tr("Unable to retrieve weather data");
    }

    emit countryChanged(response);
    emit descriptionChanged(response);
    emit tempChanged(response);
    emit humidityChanged(response);
    emit pressureChanged(response);
    emit temp_minChanged(response);
    emit temp_maxChanged(response);
    emit speedChanged(response);
    emit degChanged(response);
    emit nameChanged(response);
}

and this is the header

 

class ExternalIP : public QObject
{
    Q_OBJECT
    Q_PROPERTY (QString country READ country WRITE setCountry NOTIFY countryChanged);
    Q_INVOKABLE QString country() {
        return _country;
    }
    Q_PROPERTY (QString description READ description WRITE setDescription NOTIFY descriptionChanged);
    Q_INVOKABLE QString description(){
        return _description;
    }
    Q_PROPERTY (QString temp READ temp WRITE setTemp NOTIFY tempChanged);
    Q_INVOKABLE QString temp(){
        return _temp;
    }
    Q_PROPERTY (QString humidity READ humidity WRITE setHumidity NOTIFY humidityChanged);
    Q_INVOKABLE QString humidity(){
        return _humidity;
    }
    Q_PROPERTY (QString pressure READ pressure WRITE setPressure NOTIFY pressureChanged);
    Q_INVOKABLE QString pressure(){
        return _pressure;
    }
    Q_PROPERTY (QString temp_min READ temp_min WRITE setTemp_min NOTIFY temp_minChanged);
    Q_INVOKABLE QString temp_min(){
        return _temp_min;
    }
    Q_PROPERTY (QString temp_max READ temp_max WRITE setTemp_max NOTIFY temp_maxChanged);
    Q_INVOKABLE QString temp_max(){
        return _temp_max;
    }
    Q_PROPERTY (QString speed READ speed WRITE setSpeed NOTIFY speedChanged);
    Q_INVOKABLE QString speed(){
        return _speed;
    }
    Q_PROPERTY (QString deg READ deg WRITE setDeg NOTIFY degChanged);
    Q_INVOKABLE QString deg(){
        return _deg;
    }
    Q_PROPERTY (QString name READ name WRITE setName NOTIFY nameChanged);
    Q_INVOKABLE QString name(){
        return _name;
    }

public:
    ExternalIP(QObject* parent = 0);

public Q_SLOTS:
    void getWeatherdata();
    void setCountry(QString c){
        if (c!=_country){
            _country = c;
            emit countryChanged(c);
        }
    }
    void setDescription(QString des){
        if (des!=_description){
            emit descriptionChanged(des);
        }
    }
    void setTemp(QString t){
        if (t!=_temp){
            emit tempChanged(t);
        }
    }
    void setHumidity(QString h){
        if (h!=_humidity){
            emit humidityChanged(h);
        }
    }
    void setPressure(QString p){
    if (p!=_pressure){
        emit pressureChanged(p);
    }
    }
    void setTemp_min(QString tmin){
        if (tmin!=_temp_min){
            emit temp_minChanged(tmin);
        }
    }
    void setTemp_max(QString tmax){
        if (tmax!=_temp_max){
            emit temp_maxChanged(tmax);
        }
    }
    void setSpeed(QString s){
        if (s!=_speed){
                    emit speedChanged(s);
                }
    }
    void setDeg(QString d){
        if (d!=_deg){
            emit degChanged(d);
        }
    }
    void setName(QString n){
        if(n!=_name){
            emit nameChanged(n);
        }
    }
Q_SIGNALS:
    void countryChanged(QString);
    void descriptionChanged(QString);
    void tempChanged(QString);
    void humidityChanged(QString);
    void pressureChanged(QString);
    void temp_minChanged(QString);
    void temp_maxChanged(QString);
    void speedChanged(QString);
    void degChanged(QString);
    void nameChanged(QString);

private Q_SLOTS:
    void onGetReply();

private:
    QNetworkAccessManager* m_networkAccessManager;
    QString _country;
    QString _description;
    QString _temp;
    QString _humidity;
    QString _pressure;
    QString _temp_min;
    QString _temp_max;
    QString _speed;
    QString _deg;
    QString _name;
};

#endif

 

 

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: Problems parsing json from qbytearray

Hi,

 

Country is the only one where the set function 'does' something...

 

   if (c!=_country){
            _country = c;
            emit countryChanged(c);
        }

 

In all of the other setters - you're emitting a changed event - but not changing the value.

 

 

 

PS:

In the code that reads the values - you don't need ANY emits - because all of the individual setters take care of emitting anyway...

 

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: Problems parsing json from qbytearray

Oh I completely pass that, thank you!!
Please use plain text.
Developer
edusal100
Posts: 108
Registered: ‎09-21-2013
My Device: Blackberry Z10 STL100-1
My Carrier: Movistar

Re: Problems parsing json from qbytearray

Hi, I have it working already, but I'm having a problem with two of the values, temp and pressure, the service sometimes send me numbers with decimals, but I don't know how to round the number or hide them
Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: Problems parsing json from qbytearray

Hi,

 

You have access to standard JavaScript functionality inside QML - you could use http://www.w3schools.com/jsref/jsref_round.asp ...

 

Or - you could round it from inside your C++.

 

Or - stop using a number - and report the property as a QString. http://harmattan-dev.nokia.com/docs/library/html/qt4/qstring.html#argument-formats let you specify the number of digits after the decimal point...

 

Thank you.

 

 

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.