URL Shortener: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[URL Shortener Bulgarian|Български]]
[[Category:snippets]]<br />[[Category:HowTo]]


=<span class="caps">URL</span> Shortener=
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


==Overview==
'''English''' [[URL_Shortener_Bulgarian|Български]]


<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.
= URL Shortener =


==<span class="caps">URL</span> Shortener <span class="caps">API</span>==
== Overview ==


There are plenty of <span class="caps">URL</span> Shortner <span class="caps">API</span>. The most popular are:
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.


* [http://goo.gl/ Google Url Shortener] ''[goo.gl]''
== URL Shortener API ==
* [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==
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


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]''.
== URL Shortener for Qt ==


===Source Code===
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.


<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).
=== Source Code ===


===Usage===
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).


==Related Articles==
<code><br />#ifndef URLSHORTENER_H<br />#define URLSHORTENER_H


[[Download Data from URL|Download Data from <span class="caps">URL</span>]] ''[qt.io]''
//Standard includes<br />#include &lt;QObject&amp;gt;<br />#include &lt;QString&amp;gt;


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


* [[:Category:HowTo|HowTo]]
class UrlShortener : public QObject<br />{<br /> Q_OBJECT
* [[:Category:snippets|snippets]]
 
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 &quot;urlshortener.h&amp;quot;
 
//Standard includes<br />#include &lt;QTextStream&amp;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( &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 />//——————————————————————————
<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-&gt;requestShortUrl(QString(&quot;http://example.com&amp;quot;));<br /><code>
 
== Related Articles ==

Revision as of 10:47, 24 February 2015


[toc align_right="yes&quot; depth="2&quot;]

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&quot;:http://goo.gl/
* "Bit.ly&quot;:http://dev.bitly.com/
* "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).

<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