1

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Tjaart
  • 3,912
  • 2
  • 37
  • 61

3 Answers3

0

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.

Tjaart
  • 3,912
  • 2
  • 37
  • 61
0

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.

shilovk
  • 11,718
  • 17
  • 75
  • 74
P J
  • 1
-2

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.

hyarion
  • 2,251
  • 2
  • 17
  • 28
  • You can use an http source for JSON. https://developer.blackberry.com/cascades/documentation/device_platform/data_access/using_data_source.html – Tjaart Jan 21 '13 at 06:24
  • My apologies, I had been working with other objects (e.g. imageviews) and source requires a local path. – hyarion Jan 22 '13 at 10:34
  • I might have to use NetworkAccessManager anyway if the Qml DataSource does not support https. All the convenience of the DataSource would be out the window though, so that is an absolute last resort. – Tjaart Jan 23 '13 at 09:40