Pages

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

Wednesday, August 11, 2010

A Comfortable Online HTML Editor

A powerful Web-based WYSIWYG HTML editor that is designed for easy integration into our custom web applications


We can use this link for designing blog content HTML editor

Friday, August 6, 2010

rake aborted! Mysql::Error



When i tried to setup and migrate some objects in the MySQL DB
with Ruby on Rails.
  While executing the command  rake db:migrate ,i got the following error:
error:
       rake aborted!
       Mysql::Error: query: not connected: CREATE TABLE `schema_migrations` (`version`varchar(255) NOT NULL) ENGINE=InnoDB
(See full trace by running task with --trace)
Using rake db:create database succesfully created.
Solution to get rid of this issue
  Step -1 To get rid of the issue copy the libmySQL.dll
  Step -2 Paste it in C:\Ruby\bin of yours

Rails Application with MySQL DB

Follow the following step to work on rails application with mysql.





1:Our system should have the ruby installed .If not install the ruby from here
2: Start a Command prompt with Ruby C:\Ruby\bin
3:Enter the following command to install the special version of MySQL bindings.
      gem install mysql
You should expect a similar output like this:
  Successfully installed mysql-2.8.1.1-x86-mingw32
  1 gem installed
4:Enter the following command to install rails
     gem install rails
This is going to take a bit, since Rails and it’s dependencies takes around 2MB or so, and need to be downloaded and
installed.
Once done, expect see at the screen something like this:
  Successfully installed activSupport-2.3.2
  Successfully installed activerecord-2.3.2
  Successfully installed actionpack-2.3.2
  Successfully installed actionmailer-2.3.2
  Successfully installed activeresource-2.3.2
  Successfully installed rails-2.3.2
  6 gems installed
5:Copy the libmySQL.dll file from here
and paste it in your c:\ruby\bin location.
6:Create a rails application with mysql .Create a new Folder RailsPrg in C:

In Command prompt switch to C:\RailsPrg>
Let’s name our application appInMySQL
      rails appInMySQL --database=mysql
The --database option indicates to Rails that we want to use
MySQL instead of the default database adapter (SQLite3).
Rails will output a lot of lines when creating your application
structure, just an excerpt of what to see:
  ...
   create config/database.yml
   create config/routes.rb
   create config/locales/en.yml
   create config/initializers/backtrace_silencers.rb
   create config/initializers/inflections.rb
   create config/initializers/mime_types.rb
   create config/initializers/new_rails_defaults.rb
   create config/initializers/session_store.rb
   create config/environment.rb
   ...
7:Now Rails have configured for us the name of the database we want to use, and you can verify it in appInMySQL\config\database.yml
In that database.yml file modify as you need in the development phase
development:
   adapter: mysql
   encoding: utf8
   reconnect: false
   database: testMySql
   pool: 5
   username: username
   password: password
   host: hostname
8:In command prompt switch to C:\RailsPrg\appInMySQL> type the command
      rake db:create
Just that, simple db:create is going to connect to our MySQL server, and create the database for us.
9:Let’s verify that everything is in place, using the following command:


     ruby script\about
We can see the something like this as result:
About your application's environment
  Ruby version 1.9.1 (i386-mingw32)
  RubyGems version 1.3.4
  Rack version 1.0 bundled
  Rails version 2.3.2
  Active Record version 2.3.2
  Action Pack version 2.3.2
  Active Resource version 2.3.2
  Action Mailer version 2.3.2
  Active Support version 2.3.2
  Application root C:\RailsPrg\appInMySQL
  Environment development
  Database adapter mysql
  Database schema version 0
10:Now we can start generating the table for our application
Command to create a table & generate the pages for performing operation like inserting, deleting & modifying
Table name is cart
    ruby script\generate scaffold cart cartId:integer productInCart:string
Enter the migrate command
    rake db:migrate
11:Command to start the server ruby script\server
12:Execute http://localhost:3000/carts/ =>You can see the application page & can refine further the application