URL Shortener: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (→‎Source Code: Change c++ to cpp-qt to match edits mentioned here: https://forum.qt.io/topic/124460/qt-wiki-code-formatting)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''English''' [[URL Shortener Bulgarian|Български]]
[[Category:snippets]]
[[Category:HowTo]]
== Overview ==


=<span class="caps">URL</span> Shortener=
URL shortening is a technique in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. It is often used for simpler presentation of URLs at social networks and mobile applications.


==Overview==
== URL Shortener API ==


<span class="caps">URL</span> shortening is a technique in which a Uniform Resource Locator (<span class="caps">URL</span>) may be made substantially shorter in length and still direct to the required page. It is often used for simpler presentation of <span class="caps">URL</span>s at social networks and mobile applications.
There are plenty of URL Shortner API. The most popular are:
* [http://goo.gl/ Google Url Shortener]
* [http://dev.bitly.com/ Bit.ly]
* [http://is.gd/apishorteningreference.php is.gd and v.gd]


==<span class="caps">URL</span> Shortener <span class="caps">API</span>==
== URL Shortener for Qt ==


There are plenty of <span class="caps">URL</span> Shortner <span class="caps">API</span>. The most popular are:
Qt implementation of URL Shortener based on is.gd service and the [http://wiki.qt.io/Download_Data_from_URL file download class].


* [http://goo.gl/ Google Url Shortener] ''[goo.gl]''
=== Source Code ===
* [http://dev.bitly.com/ Bit.ly] ''[dev.bitly.com]''
* [http://is.gd/apishorteningreference.php is.gd and v.gd] ''[is.gd]''


==<span class="caps">URL</span> Shortener for Qt==
URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required.


Qt implementation of <span class="caps">URL</span> Shortener based on is.gd service and the [[Download Data from URL|file download class]] ''[qt.io]''.
<syntaxhighlight lang="cpp-qt">
#ifndef URLSHORTENER_H
#define URLSHORTENER_H


===Source Code===
//Standard includes
#include <QObject>
#include <QString>


<span class="caps">URL</span> Shortener <span class="caps">API</span> provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required. The provided implementation is from [https://gitorious.org/location2sms location2sms] ''[gitorious.org]'' (an open source mobile application).
//Project specific includes
#include "filedownloader.h"


===Usage===
class UrlShortener : public QObject
{
Q_OBJECT


==Related Articles==
private:
FileDownloader* m_pDownloader;
QString m_sShortUrl;


[[Download Data from URL|Download Data from <span class="caps">URL</span>]] ''[qt.io]''
public:
explicit UrlShortener(QObject *parent = 0);
virtual ~UrlShortener();
/*
* Request short URL from Google API
*
* param sURL
*
* return nothing
*/
void requestShortUrl(QString sURL);


===Categories:===
/*
* Get address
*
* return QString
*/
QString getShortUrl() const;


* [[:Category:HowTo|HowTo]]
signals:
* [[:Category:snippets|snippets]]
 
void shortUrlRetrieved();
 
private slots:
void parseResponse();
};
 
#endif // URLSHORTENER_H
</syntaxhighlight>
 
<syntaxhighlight lang="cpp-qt">
 
//Project specific includes
#include "urlshortener.h"
 
//Standard includes
#include <QTextStream>
 
UrlShortener::UrlShortener(QObject *parent) :
QObject(parent),
m_pDownloader(NULL)
{
m_pDownloader = new FileDownloader(this);
connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
}
// ------------------
 
UrlShortener::~UrlShortener()
{
//Nothing to do
}
// ------------------
 
void UrlShortener::requestShortUrl(QString sURL)
{
//Obtain short URL
QUrl req = QUrl( "http://is.gd/create.php");
req.addQueryItem("format", "simple");
req.addQueryItem("longurl", sURL);
m_pDownloader->downloadUrl(req);
}
// ------------------
 
void UrlShortener::parseResponse()
{
QTextStream downloadedStream(m_pDownloader->downloadedData());
 
m_sShortUrl = downloadedStream.readAll();
 
//emit a signal
emit shortUrlRetrieved();
}
// ------------------
 
QString UrlShortener::getShortUrl() const
{
return m_sShortUrl;
}
// ------------------
</syntaxhighlight>
 
=== Usage ===
<code>
m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
</code>

Latest revision as of 04:15, 7 June 2023

Overview

URL shortening is a technique in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. It is often used for simpler presentation of URLs at social networks and mobile applications.

URL Shortener API

There are plenty of URL Shortner API. The most popular are:

URL Shortener for Qt

Qt implementation of URL Shortener based on is.gd service and the file download class.

Source Code

URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required.

#ifndef URLSHORTENER_H
#define URLSHORTENER_H

//Standard includes
#include <QObject>
#include <QString>

//Project specific includes
#include "filedownloader.h"

class UrlShortener : public QObject
{
 Q_OBJECT

private:
FileDownloader* m_pDownloader;
QString m_sShortUrl;

public:
explicit UrlShortener(QObject *parent = 0);
virtual ~UrlShortener();
 /*
 * Request short URL from Google API
 *
 * param sURL
 *
 * return nothing
 */
 void requestShortUrl(QString sURL);

 /*
 * Get address
 *
 * return QString
 */
 QString getShortUrl() const;

signals:

void shortUrlRetrieved();

private slots:
void parseResponse();
};

#endif // URLSHORTENER_H
//Project specific includes
#include "urlshortener.h"

//Standard includes
#include <QTextStream>

UrlShortener::UrlShortener(QObject *parent) :
 QObject(parent),
 m_pDownloader(NULL)
{
 m_pDownloader = new FileDownloader(this);
 connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
}
// ------------------

UrlShortener::~UrlShortener()
{
 //Nothing to do
}
// ------------------

void UrlShortener::requestShortUrl(QString sURL)
{
 //Obtain short URL
 QUrl req = QUrl( "http://is.gd/create.php");
 req.addQueryItem("format", "simple");
 req.addQueryItem("longurl", sURL);
 m_pDownloader->downloadUrl(req);
}
// ------------------

void UrlShortener::parseResponse()
{
 QTextStream downloadedStream(m_pDownloader->downloadedData());

 m_sShortUrl = downloadedStream.readAll();

 //emit a signal
 emit shortUrlRetrieved();
}
// ------------------

QString UrlShortener::getShortUrl() const
{
 return m_sShortUrl;
}
// ------------------

Usage

m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));