0

在给定的代码中,我使用了一个标签和一个按钮。我希望当我单击按钮时必须发送一个请求,该请求从给定链接获取 Json 并在标签上打印。但是对于这段代码,我只是在成功后在标签内打印“OK”

我面临的问题是我没有进入 if 语句。实际上在按钮单击时没有任何反应。我知道 QT 中有我可以使用的网络管理器,但在我的情况下,我想在 QML 中解析

// Default empty project template
import bb.cascades 1.0

// creates one page with a label

Page {
    Container {
        layout: StackLayout {}
        Label {
            id: msgLabel
            text: qsTr("Hello World")
            textStyle.base: SystemDefaults.TextStyles.BigText
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }
        Button {
            id: requestXML
            objectName: "requestXML"
            onClicked: {
                var doc = new XMLHttpRequest();
                doc.onreadystatechange = function() {
                    if (doc.readyState == 4) {
                        msgLabel.text="OK"
                        // pass “doc.responseText” to C++ for Parsing
                    }
                }
                doc.open("GET", "http://www.example.com/sample.json", true);
                doc.send();

            }
    }
    }
}

在我的 .pro 文件中,我已经声明

CONFIG += qt warn_on cascades10
QT += network
CONFIG += debug
CONFIG += console
LIBS += -lbbdata
QT +=script
QT +=scripttools

我哪里错了?或者我必须声明一些别的东西

4

2 回答 2

0

我认为问题出在默认情况下不允许的跨域请求中。WebWorks您需要在您的应用程序config.xml文件中为您希望访问的域声明一个<access>元素。

不幸的是,我不知道如何在本机QML项目中做同样的事情。中的现有元素bar-descriptor.xml不允许做类似的事情。

添加:

我写了一个简单的 HTTP 存根来测试这个猜测,看起来这确实是导致问题的原因:

$ echo -e 'HTTP/1.1 200 OK\n\n' | nc -l 8080


GET /user_timeline.json?include_entities=true&include_rts=true&screen_name=ladygaga HTTP/1.1
CONTENT-TYPE: application/x-www-form-urlencoded
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en-GB,*
User-Agent: Mozilla/5.0
Host: 192.168.1.106:8080

在这种情况下,我XMLHttpRequest.status == 200在访问 Twitter 端点时得到XMLHttpRequest.status == 0了相同的代码:

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
        }
        Label {
            id: msgLabel
            text: qsTr("Hello World")
            textStyle.base: SystemDefaults.TextStyles.BigText
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }
        Button {
            id: requestXML
            objectName: "requestXML"
            onClicked: {
                sendRequest();
            }
        }
    }

    function sendRequest() {
        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange = function() {
            if (ajax.readyState === XMLHttpRequest.DONE) {
                console.log("ajax.status: " + ajax.status);
                if (ajax.status === 200) {
                    console.log("Response = " + ajax.responseText);
                } else {
                    // This is very handy for finding out why your web service won't talk to you
                    console.log("Status: " + ajax.status + ", Status Text: " + ajax.statusText);
                }
            }
        }
//        var url = "http://192.168.1.106:8080/user_timeline.json?include_entities=true&include_rts=true&screen_name=ladygaga";
        var url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=ladygaga";
        ajax.open("GET", url, true); // only async supported
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        ajax.send();
    }
}
于 2013-08-14T01:49:49.513 回答
0

我无权访问 Twitter API,因此无法为您正确测试它。但这确实适用于其他 JSON 文件。对于此示例,您只需要.pro 文件中的LIBS += -lbbdata。另外,请阅读DataSourceJsonDataAccess。如果我是你,我会通过 C++ 下载 JSON。

import bb.cascades 1.0
import bb.data 1.0

Page {
    Container {
        layout: StackLayout {
        }
        Label {
            id: msgLabel
            text: qsTr("Hello World")
            textStyle.base: SystemDefaults.TextStyles.BigText
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }
        Button {
            id: requestXML
            onClicked: {
                dataSource.load();
            }
        }
        attachedObjects: [
            DataSource {
                id: dataSource
                source: "https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=ladygaga"
                type: DataSourceType.Json

                onDataLoaded: {
                    console.log("loaded");
                    console.log(JSON.stringify(data)); //just for testing
                    console.log(data);          
                }
                onError: {
                    console.log(errorType);
                    console.log(errorMessage);
                }
            }
        ]
    }

此外,您使用的 Twitter API 不再可用,因此请检查REST API v1.1 资源,否则您将收到以下错误:

{"errors": [{"message": "The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}
于 2013-08-13T11:53:42.153 回答