URL Shortener: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:HowTo]]
[[Category:snippets]]
[[Category:HowTo]]


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
[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:<br />* &quot;Google Url Shortener&amp;quot;:http://goo.gl/<br />* &quot;Bit.ly&amp;quot;:http://dev.bitly.com/<br />* &quot;is.gd and v.gd&amp;quot;:http://is.gd/apishorteningreference.php
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 &quot;file download class&amp;quot;:http://wiki.qt.io/Download_Data_from_URL.
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 &quot;location2sms&amp;quot;:https://gitorious.org/location2sms (an open source mobile application).
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><br />#ifndef URLSHORTENER_H<br />#define URLSHORTENER_H
<code>
#ifndef URLSHORTENER_H
#define URLSHORTENER_H


//Standard includes<br />#include &lt;QObject&amp;gt;<br />#include &lt;QString&amp;gt;
//Standard includes
#include <QObject>
#include <QString>


//Project specific includes<br />#include &quot;filedownloader.h&amp;quot;
//Project specific includes
#include "filedownloader.h"


class UrlShortener : public QObject<br />{<br /> Q_OBJECT
class UrlShortener : public QObject
{
Q_OBJECT


private:
private:
Line 40: Line 51:


explicit UrlShortener(QObject '''parent = 0);
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);
virtual ~UrlShortener();
<br /> /'''*<br /> * Get address<br /> *<br /> * </code>return QString<br /> */<br /> QString getShortUrl() const;
 
/'''*
* 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<br /><code>
#endif // URLSHORTENER_H
<code>


</code>
</code>


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


//Standard includes<br />#include &lt;QTextStream&amp;gt;
//emit a signal
emit shortUrlRetrieved();
}
//——————————————————————————


UrlShortener::UrlShortener(QObject '''parent) :<br /> QObject(parent),<br /> m_pDownloader(NULL)<br />{<br /> m_pDownloader = new FileDownloader(this);
QString UrlShortener::getShortUrl() const
<br /> connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));<br />}<br />//——————————————————————————
{
<br />UrlShortener::~UrlShortener()<br />{<br /> //Nothing to do<br />}<br />//——————————————————————————
return m_sShortUrl;
<br />void UrlShortener::requestShortUrl(QString sURL)<br />{<br /> //Obtain short URL<br /> QUrl req = QUrl( &quot;http://is.gd/create.php&amp;quot;);<br /> req.addQueryItem(&quot;format&amp;quot;, &quot;simple&amp;quot;);<br /> req.addQueryItem(&quot;longurl&amp;quot;, sURL);<br /> m_pDownloader-&gt;downloadUrl(req);<br />}<br />//——————————————————————————
}
<br />void UrlShortener::parseResponse()<br />{<br /> QTextStream downloadedStream(m_pDownloader-&gt;downloadedData());
//——————————————————————————
<br /> m_sShortUrl = downloadedStream.readAll();
 
<br /> //emit a signal<br /> emit shortUrlRetrieved();<br />}<br />//——————————————————————————
<code>
<br />QString UrlShortener::getShortUrl() const<br />{<br /> return m_sShortUrl;<br />}<br />//——————————————————————————
 
<br /><code>
h3. Usage
<br />h3. Usage
 
<br /></code><br />m_pUrlShortener = new UrlShortener(this);<br />connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));<br />//'''<br />m_pUrlShortener-&gt;requestShortUrl(QString(&quot;http://example.com&amp;quot;));<br /><code>
</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:

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();

};

  1. endif // URLSHORTENER_H

//Project specific includes

  1. include "urlshortener.h"

//Standard includes

  1. 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