Vaibhav's Blog Space

  • Java
    August 4, 2008

Java API + Flickr API

Guest Author

I decided to play with Flickr API's for Java FX coding. But in between I found myself in hell and I started with Java :-). As all of you know Flickr Support hell lot of API to view Image, to search image, to search comment, Image translation, Image upload and many more. Check out here for detailed API. Now using these API's are not at all tough, because its all a game of XML.

Here I have written a small code, which do this :

1. It search one image(it can work for more than one image) from search API of Flickr.
2. It writes the search data on a XML, which I am copying at D:\\Hello1.xml.
3. And finally the code is using XML parsing techniques to get the information required for image view.
4. Then I use JDK6 feature of Desktop and open the default browser with the parsed URL. And
Congratulations, you can see the image.

Code, can look little big because of bad coding and writing lot of repetitive things :D.  

package flickrapp;
import java.awt.Desktop;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.\*;
import java.net.\*;
import java.util.\*;
public class Main {
public static void main(String args[]) throws Exception {
URLConnection uc = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e3471e67d4ac10c64055420d9b211b4f&per_page=1&text=Bangalore").openConnection();
DataInputStream dis = new DataInputStream(uc.getInputStream());
FileWriter fw = new FileWriter(new File("D:\\\\Hello1.xml"));
String nextline;
String[] servers = new String[10];
String[] ids = new String[10];
String[] secrets = new String[10];
while ((nextline = dis.readLine()) != null) {
String filename = "D:\\\\Hello1.xml";
XMLInputFactory factory = XMLInputFactory.newInstance();
System.out.println("FACTORY: " + factory);
XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename));
int i = -1;
while (r.hasNext()) {
XMLEvent event = r.nextEvent();
if (event.isStartElement()) {
StartElement element = (StartElement) event;
String elementName = element.getName().toString();
if (elementName.equals("photo")) {
Iterator iterator = element.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = (Attribute) iterator.next();
QName name = attribute.getName();
String value = attribute.getValue();
System.out.println("Attribute name/value: " + name + "/" + value);
if ((name.toString()).equals("server")) {
servers[i] = value;
System.out.println("Server Value" + servers[0]);
if ((name.toString()).equals("id")) {
ids[i] = value;
if ((name.toString()).equals("secret")) {
secrets[i] = value;
String flickrurl = "http://static.flickr.com/" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";
try {
URI uri = new URI(flickrurl);
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
if (desktop != null) {
} catch (IOException ioe) {
} catch (URISyntaxException use) {

Now see this line :

URLConnection uc = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e3471e67d4ac10c64055420d9b211b4f&per_page=1&text=Bangalore").openConnection();

Here some important thing to see :

http://api.flickr.com/services/rest/?method=flickr.photos.search    -> this is the way to write Flickr API.

api_key=e3471e67d4ac10c64055420d9b211b4f  -> required for service. This is my api_key, you can have your own. Just go to flickr service and generate your API_key

per_page=1  -> Here is what I meant one image, if you can change this to 10. It will gather information of top 10 images in XML file.

text=Bangalore  -> Sorry, I have hard coded it for now. This is the search string.

Now, look at the XML file get generated in D:\\Hello1.xml. You can see one entry with tag photo inside photos. So, we need to take some data from this XML file and add in proper style to get the correct URL and that is here:

String flickrurl = "http://static.flickr.com/" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";

Again, lot of things are hard coded(which I will correct in next post). Since only one image (i=0). I am assuming its a jpg image :D. Now calling Desktop API, you can load this image on default browser.

Now, this is still a live question, for certain keyword search, it gives the same result like when I search for keyword "Vaibhav", code and search box of Flickr provided the same result(which is not my photo :-( )  whereas if I search on things like "Bangalore", result is not similar for many cases. I don't know how Flickr handles it internally.

Probably next I will try to upload image or translate image but in Java FX :-)

Join the discussion

Comments ( 14 )
  • kodeninja Monday, August 4, 2008


    Your code just got Groovier :)

    import java.awt.Desktop

    def flickrURL =




    // flickr xml root

    def rsp = new XmlSlurper().parseText(flickrURL.toURL().text)

    // store all dah image urls here

    def imageList = []

    def flickrImageURLBase = "http://static.flickr.com/"

    // reads just like the xml :)

    rsp.photos.photo.each {

    photo ->

    imageList << flickrImageURLBase + "${photo.@server}/${photo.@id}_${photo.@secret}.jpg"


    // images galore - just showing the first one here!



  • Vaibhav Choudhary Monday, August 4, 2008

    You want to say this much is the code i need to write in Groovy ! is it so ?

  • kodeninja Monday, August 4, 2008

    You betcha, buddy!

    Download Groovy, if u haven't yet, and just run this code as is. Give it a spin and be enlightened :)



  • Vaibhav Monday, August 4, 2008

    Ya that's why we like scripting language. Ya I know little of groovy and can understand this is a powerful language ! Lets see if java fx code can make it small :-)

  • Robert Monday, August 4, 2008

    @kodeninja darn! I was going to do the same thing. Look how clean that groovy is :)

  • Glenn Brown Monday, August 4, 2008

    why not use flickrj?

  • Vaibhav Choudhary Tuesday, August 5, 2008

    oh my God but we all love Java :). So, let the code go long :D

  • suhail Saturday, October 18, 2008

    iam expert in 2d& graphic designing

  • Kilik Saturday, June 27, 2009

    I need some help, please.

    I can't import javax.xml.\* libraries. I don't touch Java and Eclipse IDE for a year. I suppose that I must add a .jar library of XML, right?

    Can anyone guide me?

    I'm a student from Spain, sorry for my english.

  • Steven Sunday, November 8, 2009

    Nice post using xml.

    Here is another example using Java 5 and flickrj.


  • siki&#351; Monday, November 23, 2009

    thanks you .thanks you .thanks you .thanks you .thanks you .

  • Tag Heuer Sunday, December 20, 2009
  • dizinet Wednesday, January 13, 2010

    Her turlu dizi ve sinema

  • guest Sunday, March 20, 2011

    Good my friend - forget those idiots who try to invent new APIs jflickr this and that

    and for those backrwards people like me who hate the word xml her's how with only strings

    public void doIt(Vector v, int k, String srch) {

    if(srch!=null) searchTerms=srch;

    try {

    URLConnection uc = new URL("http://api.flickr.com/services/rest/?"+



    DataInputStream dis = new DataInputStream(uc.getInputStream());

    FileWriter fw = new FileWriter(new File("flick1"));

    String nextline, total="";

    String[] servers = new String[10];

    String[] ids = new String[10];

    String[] secrets = new String[10];

    while ((nextline = dis.readLine()) != null) {


    System.out.println("line "+nextline);





    int ind=0, i=0;


    StrinkTokenizer st=new StringTokenizer(total, "<photo");

    while((ind=total.indexOf("<photo", ind+1))>-1) {

    int idi=total.indexOf("id=", ind);

    String id=total.substring(idi+4, total.indexOf('"', idi+4));

    int sci=total.indexOf("secret=", ind);

    String sc=total.substring(sci+8, total.indexOf('"', sci+8));

    int svi=total.indexOf("server=", ind);

    String sv=total.substring(svi+8, total.indexOf('"', svi+8));

    String url="http://static.flickr.com/"+sv+"/"+id+"_"+sc+".jpg";

    try {

    BufferedImage bi=ImageIO.read(new URL(url));

    if(v==null) paintOne(bi, 10+i\*5, 100+i);

    else v.add(bi);


    catch (IOException ex) { ex.printStackTrace(); return; }


    try { Thread.sleep(5000); } catch(InterruptedException ie) {}


    if(i>k) break;



    catch(Exception ex2) { ex2.printStackTrace(); return; }


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.