question archive C++ For this assignment you will modify the example client shown below

C++ For this assignment you will modify the example client shown below

Subject:Computer SciencePrice: Bought3

C++ For this assignment you will modify the example client shown below. Currently the client requests discussion board posts from the server and displays them. Rewrite the client so that it requests comments from the server and displays them.

Here is an example JSON object of a Comment.

   
{
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": ,
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos"
}

You will need to replace the line that sets the URL of the request to the following:

request.setUrl(QUrl("http://jsonplaceholder.typicode.com/comments/" + QString::number(commentId)));      

 

Here is the client.h

 

#ifndef CLIENT_H
#define CLIENT_H

#include<QApplication>
#include<QObject>
#include<QNetworkAccessManager>
#include<QNetworkRequest>
#include<QNetworkReply>
#include<QDebug>
#include<QJsonObject>
#include<QJsonDocument>
#include<iostream>
using namespace std;

struct ForumPost {
    int userId;
    int id;
    string title;
    string body;
};

class Client: QObject {
Q_OBJECT
private:
    QNetworkAccessManager* networkManager;
    void blockUntilReplyReceived(QNetworkReply* reply);
    unique_ptr<ForumPost> parseJson(QNetworkReply& reply);
public:
    explicit Client(QObject *parent = nullptr);
    ~Client();
    unique_ptr<ForumPost> request(int postId);
signals:
public slots:
    void onResult(QNetworkReply* reply) { }
};

#endif // CLIENT_H

 

Here is the class definition for Client.cpp.

 

#include "client.h"

/**
 * The server doesn't respond immediatly, what should we do in the mean time?
 * A QEventLoop can be used to wait in a loop until the server responds.
 *
 * @brief blockUntilReplyReceived Don't return until reply is received by server.
 * @param reply The object to wait on.
 */
void Client::blockUntilReplyReceived(QNetworkReply* reply) {
    QEventLoop loop;
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
}

/**
 * The server replies with a JSON object, to be useful, we want to map that JSON
 * object into a C++ object.
 *
 * @brief parseJson Convert the JSON object into a C++ object.
 * @param reply The reply received from the server.
 * @return A unique_ptr to a ForumPost object.
 */
unique_ptr<ForumPost> Client::parseJson(QNetworkReply& reply) {
    QString strReply = (QString) reply.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject jsonObject = doc.object();

    int userId = jsonObject.value("userId").toInt();
    int id = jsonObject.value("id").toInt();
    string title = jsonObject.value("title").toString().toStdString();
    string body = jsonObject.value("body").toString().toStdString();
    unique_ptr<ForumPost> post = make_unique<ForumPost>(userId, id, title, body);
    return post;
}

Client::Client(QObject *parent) {
    networkManager = new QNetworkAccessManager(this);
    connect(networkManager, SIGNAL(finished(QNetworkReply*)), this,
            SLOT(onResult(QNetworkReply*)));
}

Client::~Client() {
    delete networkManager;
}

/**
 * @brief request Request something from the server.
 * @param postId The postId we are requesting from the server.
 * @return A unique_ptr to a ForumPost object.
 */
unique_ptr<ForumPost> Client::request(int postId) {
    QNetworkRequest request;
    QNetworkReply* reply;

    request.setUrl(QUrl("http://jsonplaceholder.typicode.com/posts/"
                        + QString::number(postId)));
    reply = networkManager->get(request);
    blockUntilReplyReceived(reply);

    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << reply->errorString();
        return nullptr;
    }
    return parseJson(*reply);
}

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE