URL Shortener: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | [[Category:snippets]] | ||
[[Category:HowTo]] | |||
[toc align_right= | [toc align_right="yes" depth="2"] | ||
'''English''' [[URL_Shortener_Bulgarian|Български]] | '''English''' [[URL_Shortener_Bulgarian|Български]] | ||
Line 13: | Line 14: | ||
== URL Shortener API == | == URL Shortener API == | ||
There are plenty of URL Shortner API. The most popular are: | 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 == | == URL Shortener for Qt == | ||
Qt implementation of URL Shortener based on is.gd service and the | 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 === | === 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 | 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). | ||
<code> | <code> | ||
#ifndef URLSHORTENER_H | |||
#define URLSHORTENER_H | |||
//Standard includes | //Standard includes | ||
#include <QObject> | |||
#include <QString> | |||
//Project specific includes | //Project specific includes | ||
#include "filedownloader.h" | |||
class UrlShortener : public QObject | class UrlShortener : public QObject | ||
{ | |||
Q_OBJECT | |||
private: | private: | ||
Line 40: | Line 51: | ||
explicit UrlShortener(QObject '''parent = 0); | explicit UrlShortener(QObject '''parent = 0); | ||
virtual ~UrlShortener(); | |||
/'''* | |||
* Request short URL from Google API | |||
* | |||
* </code>param sURL | |||
* | |||
* <code>return nothing | |||
*/ | |||
void requestShortUrl(QString sURL); | |||
/'''* | |||
* Get address | |||
* | |||
* </code>return QString | |||
*/ | |||
QString getShortUrl() const; | |||
signals: | signals: | ||
Line 54: | Line 80: | ||
}; | }; | ||
#endif // URLSHORTENER_H | #endif // URLSHORTENER_H | ||
<code> | |||
</code> | </code> | ||
//Project specific includes | //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(); | |||
} | |||
//—————————————————————————— | |||
UrlShortener:: | QString UrlShortener::getShortUrl() const | ||
{ | |||
return m_sShortUrl; | |||
} | |||
//—————————————————————————— | |||
<code> | |||
h3. Usage | |||
</code> | |||
m_pUrlShortener = new UrlShortener(this); | |||
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl())); | |||
//''' | |||
m_pUrlShortener->requestShortUrl(QString("http://example.com")); | |||
<code> | |||
== Related Articles == | == Related Articles == |
Revision as of 11:07, 25 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).
#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;
} //——————————————————————————
h3. Usage
m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
Related Articles