URL Shortener: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Remove non-functioning "toc" command)
(Formatting fixed)
Line 26: Line 26:
=== 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 [https://gitorious.org/location2sms 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.  


<code>
<code>
Line 44: Line 44:


private:
private:
FileDownloader* m_pDownloader;
FileDownloader* m_pDownloader;
QString m_sShortUrl;
QString m_sShortUrl;


public:
public:
 
explicit UrlShortener(QObject *parent = 0);
explicit UrlShortener(QObject '''parent = 0);
virtual ~UrlShortener();
 
  /*
virtual ~UrlShortener();
 
  /'''*
  * Request short URL from Google API
  * Request short URL from Google API
  *
  *
  * </code>param sURL
  * param sURL
  *
  *
  * <code>return nothing
  * return nothing
  */
  */
  void requestShortUrl(QString sURL);
  void requestShortUrl(QString sURL);


  /'''*
  /*
  * Get address
  * Get address
  *
  *
  * </code>return QString
  * return QString
  */
  */
  QString getShortUrl() const;
  QString getShortUrl() const;
Line 76: Line 71:


private slots:
private slots:
void parseResponse();
void parseResponse();
};
};


#endif // URLSHORTENER_H
#endif // URLSHORTENER_H
</code>
<code>
<code>
</code>


//Project specific includes
//Project specific includes
Line 92: Line 85:
#include <QTextStream>
#include <QTextStream>


UrlShortener::UrlShortener(QObject '''parent) :
UrlShortener::UrlShortener(QObject *parent) :
  QObject(parent),
  QObject(parent),
  m_pDownloader(NULL)
  m_pDownloader(NULL)
{
{
  m_pDownloader = new FileDownloader(this);
  m_pDownloader = new FileDownloader(this);
  connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
  connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
}
}
//——————————————————————————
// ------------------


UrlShortener::~UrlShortener()
UrlShortener::~UrlShortener()
Line 106: Line 98:
  //Nothing to do
  //Nothing to do
}
}
//——————————————————————————
// ------------------


void UrlShortener::requestShortUrl(QString sURL)
void UrlShortener::requestShortUrl(QString sURL)
Line 116: Line 108:
  m_pDownloader->downloadUrl(req);
  m_pDownloader->downloadUrl(req);
}
}
//——————————————————————————
// ------------------


void UrlShortener::parseResponse()
void UrlShortener::parseResponse()
Line 127: Line 119:
  emit shortUrlRetrieved();
  emit shortUrlRetrieved();
}
}
//——————————————————————————
// ------------------


QString UrlShortener::getShortUrl() const
QString UrlShortener::getShortUrl() const
Line 133: Line 125:
  return m_sShortUrl;
  return m_sShortUrl;
}
}
//——————————————————————————
// ------------------
</code>


=== Usage ===
<code>
<code>
=== Usage ===
</code>
m_pUrlShortener = new UrlShortener(this);
m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//'''
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
<code>
</code>
 
== Related Articles ==

Revision as of 15:56, 24 March 2016

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.


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.

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.

#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;
}
// ------------------

Usage

m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));