Pregunta

Loading my JSON DataSource through https seems to be failing. It works when I load the url in a browser on my BlackBerry 10 device but fails when I try to use that url in Qml.

This is what my DataSource object looks like:

DataSource {
    id: dsTitles
    source: ""
    type: DataSourceType.Json
    onDataLoaded: {
        if (Common.hasError(updateError, data, "Failed to load data. Please check your connection and try again.")) {
            console.log("Data contains error");
            navigationPane.pop();
            return;
        }
        loadedTitles = true;
        Code.loadDropDown(data, ddTitle, "title", "titleId")
        Code.hideLoadIndicator();
        if (updateProfile && ddTitle.selectedValue == null) {
            Code.setDropDownOptionByValue(ddTitle, profile.userTitleId);
        }
    }
    onError: {
        console.log("Failed to load titles: " + errorMessage);
    }

Any https JSON web service can be used as an example of this problem.

¿Fue útil?

Solución

It is possible to use XMLHttpRequest to load data from JavaScript. I have created the following two functions:

function loadJsonDataList(value, dataModel) { for (var i = 0; i < value.length; i ++) { dataModel.insert(value[i]); } return value; }

function loadData(url, onComplete) { var request = new XMLHttpRequest();

request.open("GET", url);
request.send();

request.onreadystatechange = function() {
    if (request.readyState === 4 && request.status === 200) {
        onComplete(JSON.parse(request.responseText));
    } else {

        onComplete(request.responseText);
    }
};

}

You can use the functions above as follows:

function getData2() 
{
    console.log("Get data called");
    Common.loadData("https://mywebservice/here/someFunction", getDataCallBack);
}

function getDataCallBack(dataObject) 
{
    console.log("Get data callback called");
    Common.loadJsonDataList(dataObject, lstViewItems.dataModel);
}

Currently it seems like Qml DataSource objects are unable to load SSL web service data, however it is possible to load it manually as stated above. I will try to keep an eye on the issue and update the answer if they do eventually fix it or if another answer can explain how to use DataSource this way.

Otros consejos

I have solved this finally using https in my data source. By explicitly declaring the remote : true.

This now seems to solve secure JSON sources not loading.

You cannot use an http value for the source, you will need to actually download the JSON file and then pass it to the datasource.

The "Weatherguesser" example app by blackberry shows how to fetch data from an online source and pass it to your qml.

Take a look specifically at the weathermodel.cpp file which downloads weather data from a json file online.

In brief, you will need a NetworkAccessManager and then need to pass a url to it to download. The result can then either be managed in c++ or you could pass it direct to qml.

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