Pregunta

I am developing an application for BB-10 using web service. In this I want to parse JSON in both the get and post methods and I want to check the Internet availability.

How can I do this?

¿Fue útil?

Solución

Check the Internet Availability using the below code

bool app::isNetworkAvailable() {
    QNetworkConfigurationManager netMgr;
    QList<QNetworkConfiguration> mNetList = netMgr.allConfigurations(QNetworkConfiguration::Active);

    return (mNetList.count() > 0 && netMgr.isOnline());
}

Otros consejos

My teacher created a qml component that shows if there is connection and what kind of connection it is (wifi, bluetooth, carrier etc). It also sends a signal when the connection status or the interface used has changed.

The code is hosted at github: https://github.com/rodrigopex/CheckInternetMicroSample

1.HPP FILE

class controller : public QObject
{
 Q_OBJECT
public:
 controller(bb::cascades::Application *app);


public Q_SLOTS:
 void sendRequest(const QString &countryID);


private Q_SLOTS:

void onFinished();

};

2.CPP FILE

void controller::sendRequest(const QString &countryID)
{

QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);

const QString queryUri = QString::fromLatin1("http://192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=%1").arg(countryID);

QNetworkRequest request(queryUri);

QNetworkReply* reply = networkAccessManager->get(request);

bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
 Q_ASSERT(ok);
 Q_UNUSED(ok);
}


void controller::onFinished()
{
 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 QString response;
 if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
 {
 JsonDataAccess jda;
 QVariantMap map = jda.loadFromBuffer(reply->readAll()).toMap();

QVariantList addresses = map["GetCategoryResult"].toList();

foreach(QVariant var, addresses) {
 QVariantMap addressMap = var.toMap();

qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
 qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
 }
 }
 else {
 qDebug() << "Server returned code " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
 }
 }

SEE FULL CODE HERE----> http://supportforums.blackberry.com/t5/Native-Development/webservice-help-json/m-p/2569953/highlight/false#M46724

1) You can check the internet available by the following method as per documentation:

bool QNetworkConfigurationManager::isOnline () const

Returns true if the system is considered to be connected to another device via an active network interface; otherwise returns false.

2) As for the json bits, you could use the json parser in Qt 5 as follows:

JSON Support in Qt

It is simple enough to bundle Qt 5 againt your application, but it will hopefully available on the platform soon.

Qt 5 on BlackBerry 10 - Beyond the Myth

Failing that, it would be very simple to backport those few classes to Qt 4.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top