2012-08-24 2 views
0

Blackberry 10 Cascades, QML 및 C++ QT에서 작업 중이며 작성한 작은 PHP 웹 서비스에서 XML 데이터를 가져 오려고합니다. 내가 사용하고있는 Blackberry 10 Dev Alpha Simulator의 목록 - 그러나 작동하지 않습니다.XML 데이터를 Blackberry 10의 listItemComponents에 표시합니다. Cascades QML

즉, XML 데이터가 QML 문서의 ListView에로드되어 Blackberry Simulator의 화면에 표시되지 않습니다. 그 일을하는 데 도움이 필요해.

일반적인 http 요청과 관련된 예제로 시작하여 내 용도에 맞게 사용자 정의되도록 수정했습니다 (http 게시 요청). (이 코드는 텍스트 필드에서 세대 번호 (1-5)를 취하고 해당 세대의 포켓몬 게임 색상 목록을 인쇄합니다).

내가 함께 시작 QML 파일입니다

import bb.cascades 1.0 

TabbedPane { 
    activePane: Page { 
     actions: [ 
      // An action item that calls the C++ function that retrieves 
      // the contact list 
      ActionItem { 
       title: "Refresh" 
       onTriggered: app.initiateRequest() 
      } 
     ]  
     content: Container { 
      layout: DockLayout {} 

      // A list that has two list item components, one for a header 
      // and one for contact names. The list has an object name so 
      // that we can set the data model from C++. 
      ListView { 
       objectName: "list" 
       layout: FlowListLayout { 
        topPadding: 6 
        rightPadding: 6 
        bottomPadding: 6 
        leftPadding: 6 
       } 
       // A simple data model is loaded with just a header. 
       // This will be replaced when we load the real one 
       // from C++. 
       dataModel: XmlDataModel { 
        source: "model.xml" 
       } 

       listItemComponents: [ 
        // The header list item displays a title along with a counter 
        // that displays the number of children 
        ListItemComponent { 
         type: "header" 
         HeaderListItem { 
          topMargin: 8 
          title: ListItemData.title 
          subtitle: (ListItem.initialized ? 
           ListItem.view.dataModel 
            .childCount(ListItem.indexPath) : 0); 
         } 
        }, 
        // The contact list item displays the name of the contact 
        ListItemComponent { 
         type: "contacts" 
         StandardListItem { 
          title: ListItemData.title 
         } 
        } 
       ] 
      } 
      // The activity indicator has an object name set so that 
      // we can start and stop it from C++ 
      ActivityIndicator { 
       objectName: "indicator" 
       layoutProperties: DockLayoutProperties { 
        verticalAlignment: VerticalAlignment.Fill 
        horizontalAlignment: HorizontalAlignment.Fill 
       } 
      } 
     } // Ends the root Container 
    } // Ends the Page 
} // Ends the TabbedPane 

그리고 이것은 내 QML 파일입니다

import bb.cascades 1.0 

TabbedPane { 
    activePane: Page { 
     actions: [ 
      // An action item that calls the C++ function that retrieves 
      // the contact list 
      ActionItem { 
       title: "Refresh" 
       onTriggered: app.initiateRequest(txtGen.text) 
      } 
     ]  
     content: Container { 
      layout: StackLayout {} 

      Button { 
       text: "Get Games" 
       onClicked: app.initiateRequest(txtGen.text) 
      } 

      Label { 
       text: "Enter Generation (1-5)" 
      } 

      TextField { 
       id: txtGen 
      } 

      // A list that has two list item components, one for a header 
      // and one for contact names. The list has an object name so 
      // that we can set the data model from C++. 
      ListView { 
       objectName: "list" 
       layout: FlowListLayout { 
        topPadding: 6 
        rightPadding: 6 
        bottomPadding: 6 
        leftPadding: 6 
       } 
       // A simple data model is loaded with just a header. 
       // This will be replaced when we load the real one 
       // from C++. 
       dataModel: XmlDataModel { 
        source: "model.xml" 
       } 

       listItemComponents: [ 
        // The header list item displays a title along with a counter 
        // that displays the number of children 
        ListItemComponent { 
         type: "games" 
         HeaderListItem { 
          topMargin: 8 
          title: ListItemData.generation 
          subtitle: (ListItem.initialized ? 
           ListItem.view.dataModel 
            .childCount(ListItem.indexPath) : 0); 
         } 
        }, 
        // The contact list item displays the name of the contact 
        ListItemComponent { 
         type: "game" 
         StandardListItem { 
          title: ListItemData.title 
         } 
        } 
       ] 
      } 
      // The activity indicator has an object name set so that 
      // we can start and stop it from C++ 
      ActivityIndicator { 
       objectName: "indicator" 
       layoutProperties: DockLayoutProperties { 
        verticalAlignment: VerticalAlignment.Fill 
        horizontalAlignment: HorizontalAlignment.Fill 
       } 
      } 
     } // Ends the root Container 
    } // Ends the Page 
} // Ends the TabbedPane 

가 나는 또한 내 프로젝트의 자산/model.xml에 저장하는 XML 폴더가 다음 내용을 디렉토리, 는 :

또한
<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games> 
    </games> 
</xml> 

, 여기에 App.cpp 코드 I입니다 쓴 : 여기

#include "app.hpp" 

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 
#include <bb/cascades/Button> 
#include <bb/cascades/TextField> 
#include <QDir> 

using namespace bb::cascades; 

App::App() 
{ 
    // Load the QML document and retrieve the root node 
    QmlDocument *qml = QmlDocument::create("main.qml"); 
    mRoot = qml->createRootNode<AbstractPane>(); 

    // Retrieve the activity indicator from QML so that we can start 
    // and stop it from C++ 
    mActivityIndicator = mRoot->findChild<ActivityIndicator*>("indicator"); 

    // Retrieve the list so we can set the data model on it once 
    // we retrieve it 
    mListView = mRoot->findChild<ListView*>("list"); 

    //mTextField = mRoot->findChild<TextField*>("textField"); 

    //qDebug() << "Generation: " << mTextField->text(); 

    // Expose this class to QML so that we can call its functions from there 
    qml->setContextProperty("app", this); 

    // Create a network access manager and connect a custom slot to its 
    // finished signal 
    mNetworkAccessManager = new QNetworkAccessManager(this); 

    Q_ASSERT(connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)))); 

    // Displays a warning message if there's an issue connecting the signal 
    // and slot. This is a good practice with signals and slots as it can 
    // be easier to mistype a slot or signal definition 
    //Q_ASSERT(result); 
    //Q_UNUSED(result); 

    // Create a file in the application's data directory 
    mFile = new QFile("data/model.xml"); 

    // Set the scene using the root node 
    Application::setScene(mRoot); 
} 

void App::initiateRequest(QString text) 
{ 
     // Start the activity indicator 
    mActivityIndicator->start(); 

    // Create and send the network request 
    QNetworkRequest request = QNetworkRequest(); 
    request.setUrl(QUrl("http://192.168.1.109/TESTWEBSERVICE/MAKEXML.php")); //https://developer.blackberry.com/cascades/files/documentation/device_platform/networking/model.xml")); 
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); 

    QByteArray bytes; 
    QUrl params; 

    params.addQueryItem(QString::fromStdString("generation"), text); 

    bytes = params.encodedQuery(); 
    mNetworkAccessManager->post(request, bytes); 
} 

void App::requestFinished(QNetworkReply* reply) 
{ 
    // Check the network reply for errors 
    if (reply->error() == QNetworkReply::NoError) { 

     // Open the file and print an error if the file cannot be opened 

     if (!mFile->open(QIODevice::ReadWrite)) 
     { 
      qDebug() << "\n Failed to open file"; 
      return; 
     } 

     mFile->resize(0); 

     // Write to the file using the reply data and close the file 
     QByteArray xml = reply->readAll(); 
     mFile->write(xml); 
     qDebug() << xml; 

     mFile->flush(); 
     mFile->close(); 

     // Create the data model using the contents of the file. The 
     // location of the file is relative to the assets directory. 
     XmlDataModel *dataModel = new XmlDataModel(); 

     dataModel->setSource(QUrl("../../../data/model.xml")); 

     // Set the new data model on the list and stop the activity indicator 
     mListView->setDataModel(dataModel); 
     mActivityIndicator->stop(); 
    } 
    else 
    { 
     qDebug() << "\n Problem with the network"; 
     qDebug() << "\n" << reply->errorString(); 
    } 
} 

그리고를 내 APP_H, 파일 :

#ifndef APP_H 
#define APP_H 

#include <QObject> 
#include <QFile> 
#include <bb/cascades/ActivityIndicator> 
#include <bb/cascades/ListView> 
#include <bb/cascades/XMLDataModel> 
#include <bb/cascades/AbstractPane> 
#include <bb/cascades/TextField> 

using namespace bb::cascades; 

/*! 
* @brief Application GUI object 
*/ 
class App : public QObject 
{ 
    Q_OBJECT 

public: 
    /*! 
    * Constructor. 
    */ 
    App(); 
    /*! 
    * Initiates the network request. 
    */ 
    Q_INVOKABLE void initiateRequest(QString text); 

private slots: 
    /*! 
    * Handles the network reply. 
    */ 
    void requestFinished(QNetworkReply* reply); 

private: 
    AbstractPane *mRoot; 
    ActivityIndicator *mActivityIndicator; 
    ListView *mListView; 
    TextField *mTextField; 
    QNetworkAccessManager *mNetworkAccessManager; 
    QFile *mFile; 
    QString apiKey; 
    QString apiString; 
}; 

#endif // ifndef APP_H 

그리고 생성 paramater에 대해 다음 1 인 아웃 QDebug() 스트림 인쇄 :

"<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games generation="1"> 
    <game title="green">green</game> 
    <game title="red">red</game> 
    <game title="blue">blue</game> 
    <game title="yellow">yellow</game> 
    </games> 
</xml>" 

들어 세대 매개 변수가 2 :

"<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games generation="2"> 
    <game title="gold">gold</game> 
    <game title="silver">silver</game> 
    <game title="crystal">crystal</game>  
    </games> 
</xml>" 
생성 paramater 3을 줘서: 생성 paramater 들어

"<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games generation="3"> 
    <game title="ruby">ruby</game> 
    <game title="sapphire">sapphire</game> 
    <game title="emerald">emerald</game>  
    </games> 
</xml>" 

4 인 : 생성 paramater 5 되

"<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games generation="4"> 
    <game title="perl">perl</game> 
    <game title="diamond">diamond</game> 
    <game title="platinum">platinum</game> 
    </games> 
</xml>" 

: paramater 생성 푸이다 아무것도 대해서는

"<?xml version="1.0" encoding="utf-8"?> 
<xml> 
    <games generation="5"> 
    <game title="black">black</game> 
    <game title="white">white</game> 
    <game title="black 2">black 2</game> 
    <game title="white 2">white 2</game>  
    </games> 
</xml>" 

(foo는 자리 표시 자 임) :

나는 이것이 확실하게 작동하지 않는 이유를 잘 모른다. 다른 예제를 검색해 보았는데 코드를 제대로 작동시키는 방법을 알아낼 수 있는지 알아보기 위해 노력했지만 효과가 없었다.

나는이 질문이 길다는 것을 알고 있지만, 최선의 답변을 얻으려면 내가하고있는 일에 대해 자세히 설명하고 싶었다. 즉, 나는 정말로 내 main.cpp 파일이 관련 있다고 생각하지 않는다. 그래서 누군가가 그것을 보도록 요청하지 않는다면 나는 여기에 넣지 않을 것이다.

는 (I는

<game title="yellow">yellow</game> 

<game>yellow</game> 

으로 돌리는 것과, 내 XML 코드에서 내 게임 태그에 title 속성을 추가하고 해당 태그에 주요 데이터 값과 같은 값을 넣어 그게 문제가 해결 될지도 모른다고 생각했기 때문에 이전의 title 속성과 함께 작동시키려는 시도를 한 후에, 비록 조금은 익숙하지 않았지만 그것을하지 못했습니다. 이상적으로, 이것을 얻는 방법을 알아 내고 싶습니다. 게임 태그에 title 속성의 유무와 관계없이 두 가지 방식으로 작업 할 수 있습니다.

답변

0

문제점을 발견했습니다. 나는 나의 PHP는 웹 서비스에서

header('Content-type: text/xml'); 
"<?xml version="1.0" encoding="utf-8"?> 

를 XML 헤더 코드를 제거했다 그리고 나는 내 데이터/model.xml 파일에서이를 제거해야 :

<?xml version="1.0" encoding="utf-8"?> 

그리고 파일에 그냥이 코드를 :

<xml> 
    <games> 
    </games> 
</xml> 

QML 문서의 태그간에 데이터를 가져 오는 방법은 아직 확실하지 않습니다. 지금은 태그의 "title"속성과 함께 ListItemData.title을 사용하고 있지만 시작 및 종료 태그 사이에 내용을 원합니다.

+0

태그 게임의 'title'속성은 이라는 내부 태그와 동일하게 작동합니다. 그래서, my_title과 같습니다. –

관련 문제