Pages

Showing posts with label Bitly. Show all posts
Showing posts with label Bitly. Show all posts

Monday, February 21, 2011

Bitly using Mashape

Mashape is an API marketplace where service providers and individual developers can instantly make available an API.Its FREE to use.

Mashape can used in 2 ways

1. Making use of the featured API’s available in Mashape
2. To generate our own API using Mashape.We can upload our API in Mashape & push it into cloud.They are providing the infrastructure for generating the API.

I tried out the first way, which I need currently to work on
Check the featured services provided by Mashape -Explore.
They are in ready to use state.Avaiable in Ruby, Java, Python, PHP Obj-C, and JSON

I tried out with SimpleGeo and Bitly.

Steps to work on Bitly using Mashape :

Step1: SignIn Mashape.Get the API key.It's an automatically generated key which allows you to use the Mashape API from an application. [Ex:API key will be like this APP Key : sgFFDCfgEOKsLMsO9X7l75gCI]


Step2: Download the featured Bitly API from Mashape .
The Jar will contain Bitly component's Java client library will contain the following files:

- mashapeClient.jar - This file to be include in the project, that contains the core
functionalities of every Java client library and that is
immutable for every component.
- Bitly.java - The auto-generated *.java file for the component, that
declares a Bitly class.
- LICENSE - README

Step3 Download the following jars to work on all the above API's using Mashape.

- httpclient-4.0.jar
- httpcore-4.0-beta3.jar
- json-rpc-1.0.jar
- json-20080701.jar

Step4 Signup in Bitly & get the bitly-login and bitly-Api-key.

Step5 Code:

//Enter Mashape API key
Bitly bitly=new Bitly("sgFFDCfgEOKsLMsO9X7l75gCI ");

//Pass the url to be shortened, bitly-login and bitly-Api-key &it returens the JSONObject

JSONObject bitlyResponse = bitly.getShortenedUrl("http://sundarishree.blogspot.com/","shree","R_08de4b3475dd5655818230c15c4966471");


Advantage of this Mashape is no need to download the API jar from different sites. We can get in one point.

Thursday, August 26, 2010

Shortening URL in Java using Bitly

Bitly
Shortening any url in Java using bitly API .
We need to get API key first using bit.ly account

In two ways we can do
1. By calling the bit.ly's REST API.
2. By using bitly core methods.

1. By calling the bit.ly's REST API

We need the following jars to work on

bitlyj-2[1].0.0
commons-httpclient-3.1
commons-codec-1.2
logging-1.0.4

Code to shorten the url

HttpClient httpclient = new HttpClient();
HttpMethod method = new GetMethod("http://api.bit.ly/shorten");
NameValuePair[] valuePair=new NameValuePair[]{new NameValuePair("longUrl","http://www.sundarishree.blogspot.com/"),
new NameValuePair("version","2.0.1"),
new NameValuePair("login","YourUsername"),
new NameValuePair("apiKey","YourApikey"),
new NameValuePair("format","xml"),
new NameValuePair("history","1")
};
method.setQueryString(valuePair);
try {
httpclient.executeMethod(method);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String responseXml =null;
try {
responseXml = method.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(responseXml);


When the above code is excuted we will get the output in following XML format


<bitly>
<errorCode>0</errorCode>
<errorMessage></errorMessage>
<results>
<nodeKeyVal>
<shortKeywordUrl></shortKeywordUrl>
<hash>bzXiN2</hash>
<userHash>ceCaRl</userHash>
<nodeKey><![CDATA[http://www.sundarishree.blogspot.com/]]></nodeKey>
<shortUrl>http://bit.ly/ceCaRl</shortUrl>
<shortCNAMEUrl>http://bit.ly/ceCaRl</shortCNAMEUrl>
</nodeKeyVal>
</results>
<statusCode>OK</statusCode>
</bitly>


Code to get the short url from XML output

if(responseXml != null) {
// parse the XML
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
StringReader st = new StringReader(responseXml);
Document d = null;
String retVal=null;
try {
d = db.parse(new InputSource(st));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NodeList nl = d.getElementsByTagName("shortUrl");
if(nl != null) {
Node n = nl.item(0);
retVal = n.getTextContent();
System.out.println("Short URL >> "+retVal);
}
}


We can get the output as Short URL >>http://bit.ly/ceCaRl

2. By using bitly core methods

bitlyj-2[1].0.0 and commons-httpclient-3.1 jars are enough to use bitly core methods

Code to shorten the url


While importing Bitly mention as static

import static com.rosaloves.bitlyj.Bitly.*;
....
Provider bitly = Bitly.as("YourUserName", "YourApiKey");
ShortenedUrl info =bitly.call(shorten("http://www.visiontss.com/"));
System.out.println("Shorten URL "+info.getShortUrl());

output : Shorten URL http://bit.ly/ahyvK0

Bulk Methods
Some bitly methods support multiple arguments.info and clicks for example can take an arbitrary number of hash
or shortUrl arguments. You can do this in bitlyj just like you'd expect:

for(UrlInfo info1 : bitly.call(info("http://bit.ly/ceCaRl","http://bit.ly/ahyvK0"))) {
System.out.println("Created by :"
+info1.getCreatedBy());
System.out.println("ShortUrl :"
+info1.getUrl());
System.out.println("Title of the page :"
+info1.getTitle());
}

output :
Created by :sundarishree
ShortUrl :Url [shortBase=http://bit.ly/, globalHash=7haiBr, longUrl=, shortUrl=http://bit.ly/ahyvK0, userHash=ahyvK0]
Title of the page :Vision Tech Solutions - Software Product Development Company
Created by :sundarishree
ShortUrl :Url [shortBase=http://bit.ly/, globalHash=bzXiN2, longUrl=, shortUrl=http://bit.ly/ceCaRl, userHash=ceCaRl]
Title of the page :TECH.BLOG

You can know more in QuickStart