URL Shortener: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[[Category:HowTo]] | |||
= | [toc align_right="yes&quot; depth="2&quot;] | ||
'''English''' [[URL_Shortener_Bulgarian|Български]] | |||
= URL Shortener = | |||
== | == 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:<br />* "Google Url Shortener&quot;:http://goo.gl/<br />* "Bit.ly&quot;:http://dev.bitly.com/<br />* "is.gd and v.gd&quot;:http://is.gd/apishorteningreference.php | |||
== URL Shortener for Qt == | |||
Qt implementation of URL Shortener based on is.gd service and the "file download class&quot;:http://wiki.qt.io/Download_Data_from_URL. | |||
=== 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. The provided implementation is from "location2sms&quot;:https://gitorious.org/location2sms (an open source mobile application). | |||
<code><br />#ifndef URLSHORTENER_H<br />#define URLSHORTENER_H | |||
//Standard includes<br />#include <QObject&gt;<br />#include <QString&gt; | |||
//Project specific includes<br />#include "filedownloader.h&quot; | |||
* | class UrlShortener : public QObject<br />{<br /> Q_OBJECT | ||
private: | |||
FileDownloader* m_pDownloader; | |||
QString m_sShortUrl; | |||
public: | |||
explicit UrlShortener(QObject '''parent = 0); | |||
<br /> virtual ~UrlShortener(); | |||
<br /> /'''*<br /> * Request short URL from Google API<br /> *<br /> * </code>param sURL<br /> *<br /> * <code>return nothing<br /> '''/<br /> void requestShortUrl(QString sURL); | |||
<br /> /'''*<br /> * Get address<br /> *<br /> * </code>return QString<br /> */<br /> QString getShortUrl() const; | |||
signals: | |||
void shortUrlRetrieved(); | |||
private slots: | |||
void parseResponse(); | |||
}; | |||
#endif // URLSHORTENER_H<br /><code> | |||
</code> | |||
//Project specific includes<br />#include "urlshortener.h&quot; | |||
//Standard includes<br />#include <QTextStream&gt; | |||
UrlShortener::UrlShortener(QObject '''parent) :<br /> QObject(parent),<br /> m_pDownloader(NULL)<br />{<br /> m_pDownloader = new FileDownloader(this); | |||
<br /> connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));<br />}<br />//—————————————————————————— | |||
<br />UrlShortener::~UrlShortener()<br />{<br /> //Nothing to do<br />}<br />//—————————————————————————— | |||
<br />void UrlShortener::requestShortUrl(QString sURL)<br />{<br /> //Obtain short URL<br /> QUrl req = QUrl( "http://is.gd/create.php&quot;);<br /> req.addQueryItem("format&quot;, "simple&quot;);<br /> req.addQueryItem("longurl&quot;, sURL);<br /> m_pDownloader->downloadUrl(req);<br />}<br />//—————————————————————————— | |||
<br />void UrlShortener::parseResponse()<br />{<br /> QTextStream downloadedStream(m_pDownloader->downloadedData()); | |||
<br /> m_sShortUrl = downloadedStream.readAll(); | |||
<br /> //emit a signal<br /> emit shortUrlRetrieved();<br />}<br />//—————————————————————————— | |||
<br />QString UrlShortener::getShortUrl() const<br />{<br /> return m_sShortUrl;<br />}<br />//—————————————————————————— | |||
<br /><code> | |||
<br />h3. Usage | |||
<br /></code><br />m_pUrlShortener = new UrlShortener(this);<br />connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));<br />//'''<br />m_pUrlShortener->requestShortUrl(QString("http://example.com&quot;));<br /><code> | |||
== Related Articles == |
Revision as of 10:47, 24 February 2015
[toc align_right="yes" depth="2"]
English Български
URL Shortener
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:
* "Google Url Shortener":http://goo.gl/
* "Bit.ly":http://dev.bitly.com/
* "is.gd and v.gd":http://is.gd/apishorteningreference.php
URL Shortener for Qt
Qt implementation of URL Shortener based on is.gd service and the "file download class":http://wiki.qt.io/Download_Data_from_URL.
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. The provided implementation is from "location2sms":https://gitorious.org/location2sms (an open source mobile application).
<br />#ifndef URLSHORTENER_H<br />#define URLSHORTENER_H
//Standard includes<br />#include <QObject&gt;<br />#include <QString&gt;
//Project specific includes<br />#include "filedownloader.h&quot;
class UrlShortener : public QObject<br />{<br /> Q_OBJECT
private:
FileDownloader* m_pDownloader;
QString m_sShortUrl;
public:
explicit UrlShortener(QObject '''parent = 0);
<br /> virtual ~UrlShortener();
<br /> /'''*<br /> * Request short URL from Google API<br /> *<br /> *
param sURL
*
*
return nothing<br /> '''/<br /> void requestShortUrl(QString sURL);
<br /> /'''*<br /> * Get address<br /> *<br /> *
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;
}
//——————————————————————————
<br />h3. Usage
<br />
m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
Related Articles