URL Shortener

From Qt Wiki
Revision as of 10:47, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


[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 &lt;QObject&amp;gt;<br />#include &lt;QString&amp;gt;

//Project specific includes<br />#include &quot;filedownloader.h&amp;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();

};

  1. endif // URLSHORTENER_H

//Project specific includes
#include "urlshortener.h&quot;

//Standard includes
#include <QTextStream&gt;

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&quot;);
req.addQueryItem("format&quot;, "simple&quot;);
req.addQueryItem("longurl&quot;, 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&quot;));

Related Articles