Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Twitter search by JSON respond
Forum Updated to NodeBB v4.3 + New Features

Twitter search by JSON respond

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muhammad
    wrote on last edited by
    #1

    Hello every body
    I'm learning how to do a simple twitter search using JSON as respond,every thing works just fine, except that the text of the tweet doesn't appear.

    Here's the code

    connector.h
    @#ifndef CONNECTOR_H
    #define CONNECTOR_H
    #include <QNetworkAccessManager>
    #include <QObject>

    class connector : public QObject
    {
    Q_OBJECT
    public:
    explicit connector(QObject *parent = 0);
    Q_INVOKABLE void search(QString query);
    signals:
    void searchSignal(QString jsonSearch);
    public slots:
    void searchResultGot();
    private:
    QNetworkAccessManager *nm;
    };

    #endif // CONNECTOR_H
    @

    connector.cpp
    @#include "connector.h"
    #include<QNetworkReply>
    #include<QNetworkRequest>
    #include<QDebug>
    connector::connector(QObject *parent) :
    QObject(parent)
    {
    nm = new QNetworkAccessManager(this);
    }

    void connector::search(QString query)
    {
    QString request = "http://search.twitter.com/search.json?from=";
    request.append(query);
    QNetworkReply *rep = nm->get(QNetworkRequest(request));
    connect(rep,SIGNAL(finished()),this,SLOT(searchResultGot()));
    }

    void connector::searchResultGot()
    {
    QNetworkReply *rep = qobject_cast<QNetworkReply *>(sender());
    if(rep->error() == QNetworkReply::NoError)
    {
    QByteArray bytes = rep->readAll();
    emit searchSignal(QString(bytes));
    }
    else {
    qDebug()<<"Baso!,Erro while downloading Data"<<rep->errorString();
    }
    }
    @

    main.cpp
    @#include"connector.h"
    #include<QDeclarativeView>
    #include<Qdeclarative.h>
    #include<QApplication>
    int main (int argc, char**argv)
    {
    QApplication app(argc,argv);
    QDeclarativeView view;
    qmlRegisterType<connector>("Module",1,0,"Connector");
    view.setSource(QString("main.qml"));
    view.show();
    return app.exec();
    }
    @

    LineEdit.qml
    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    id: wrapper
    property alias text: input.text
    width: 300; height: 40;
    y: 10; radius: 25; border {color: 'black'; width: 1} smooth: true
    TextInput {
    focus: true
    id: input
    anchors.centerIn: parent
    width: parent.width - 50
    }
    Text {
    visible: input.text == ''; color: 'gray'
    text: 'enter text'; anchors.verticalCenter: parent.verticalCenter
    x: 25
    }
    }
    @

    main.qml
    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import Module 1.0
    Item {
    id: root
    width: 360; height: 640
    Connector{
    id:myConnector

        onSearchSignal: {
            var jsonObj = JSON.parse(jsonSearch)
            var tweetsArray = jsonObj.results
            var array = Array
            var array2 = Array
            for(var i = 0; i < tweetsArray.length; i++){
                tweetsModel.append(tweetsArray[i])
            }
        }
    }
    LineEdit {
        focus: true
        id: searchInput
        anchors.top: parent.top; anchors.left: parent.left; anchors.leftMargin: 15
        width: 240
    }
    Rectangle {
        id: searchButton
        width: 80; height: 40; color: 'lightBlue'; radius: 15
        anchors {top: parent.top; right: parent.right; rightMargin: 15}
        MouseArea {
            anchors.fill: parent
            onClicked: {
                if(searchInput.text != ''){
                    myConnector.search(searchInput.text)
                    tweetsModel.clear() // to handle a search after another one
                }
            }
        }
        Text{
            anchors.centerIn: parent; text: 'search'
        }
    }
    ListModel {
        id: tweetsModel
    }
    ListView{
        width: parent.width; anchors.top: searchButton.bottom; anchors.topMargin: 25; anchors.bottom: parent.bottom
        model: tweetsModel; clip: true; cacheBuffer: 1200
        delegate: Rectangle{
            id: thum
            color: "lightgray"
            border.color: "black"; border.width: 1
            radius: 20
            width: parent.width-1
            height: 80
            Image {
                id: profilePic
                source: profile_image_url
                anchors{/*left: parent.left;*/ verticalCenter: parent.verticalCenter; leftMargin: 10; horizontalCenter: textname.horizontalCenter}
                smooth: true
            }
            Text {
                id: textname
                anchors{top: parent.top; bottom: profilePic.top; left: parent.left; leftMargin: 15}
                text: qsTr(from_user_name)
            }
            Text {
                id: texttweet
                anchors{left: profilePic.right; verticalCenter: profilePic.verticalCenter; leftMargin: 15}
                text: qsTr(text)
                color: "blue"
            }
        }
    }
    

    }
    @

    I don't have endpoint, I just have Checkpoints.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      deimos
      wrote on last edited by
      #2

      hi Baso,

      I think that your query doesn't work. I tryed that but always had 0 results, so no output.

      Instead of

      bq. QString request = "http://search.twitter.com/search.json?from=";

      try with

      bq. QString request = "http://search.twitter.com/search.json?q=";

      Marco

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Muhammad
        wrote on last edited by
        #3

        marcoB
        Thanks for reply, but the app is not to search for persons on twitter, it's about getting the tweet of a specific person.
        What I want to do is to write the twitter name of someone and click search, and the app displays the last 15 tweets of that person, So I think the query is write because I get the profile pic of the name on the search, but not the tweets that's it.
        any tip about how to get that tweets from the JSON.
        Thanks

        I don't have endpoint, I just have Checkpoints.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deimos
          wrote on last edited by
          #4

          I tryed your source and the workflow does the job. Whats wrong is the twitter query you are using. With that jsonObj.results will be always empty. I never used twitter APIs, but you can find docs about getting user tweets "here":https://dev.twitter.com/docs/api/1/get/statuses/user_timeline.

          I think you shoud use something like:

          bq. QString request = "http://api.twitter.com/1/statuses/user_timeline.json?count=5&screen_name=";

          in your search() function, but the json structure is different, so no jsonObj.results
          To better undestand how is structured your query result and manage your code, try to use your browser and point it to

          bq. http://api.twitter.com/1/statuses/user_timeline.xml?count=5&screen_name=<USER>

          here I used xml instead of json because for example in my browser, the xml format is formatted and more readable.

          hope this helps

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Muhammad
            wrote on last edited by
            #5

            I'v used your query and made the required changes, but the same here I get the profile pic and the user name, but not the text of the tweet
            But this time I got error here's it

            @Error: qsTr(): first argument (text) must be a string@

            I don't know why it can't see the text as a string, may be it's about coding or something!, I don't know.
            And about the XML , I know XML much easier, but I wanna do it in JSON.
            Thanks :)

            I don't have endpoint, I just have Checkpoints.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tomma
              wrote on last edited by
              #6

              Your first version does work. Problem is main.qml:70: text: qsTr(text)
              You are trying to set Text element property text to text which loops back to same property.
              change it to: tweetsModel.get(index).text

              Also I see no reason to use qsTr() for strings which get from network request.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Muhammad
                wrote on last edited by
                #7

                Tomma, Thank you, this really fixed the problem
                Thank you

                I don't have endpoint, I just have Checkpoints.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved