Flickr api, random image goodness
Lately we have been doing all kinds of demo projects, and tests, where we did not want to use constant data to present our products. Having a demo of your application is only nice, if it has relevant data in it. It could be top class technology, but if it is using the same image 20 times in a list, well that does not look very good.
Getting 20 relevant images on a subject takes up a lot of time, even with search engines and royalty free photo sites.Luckily most search able sites have free apis, here is a little snippet for flickr's services.
The Flickr api has a lot of useful methods, but I only needed one, which is search. I choose the json response, because that's the easiest to work with. It has support for several other protocols/formats.
One problem that I encountered is, that the flickr api does not send a valid json response:
- jsonFlickrApi({"photos":{"page":1, "pages":73442, "perpage":1, "total":"73442", "photo":[{"id":"5138524515", "owner":"12766047@N06", "secret":"e000d9791e", "server":"4045", "farm":5, "title":"Gifts under $15 CAD", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"})
As you can see, it has some junk at the beginning "jsonFlickrApi(" and at the end ")" which needs to be cut off.
- //you also need your own api key
- private static final String FLICKRAPIKEY = "http://www.flickr.com/se
rvices/api/keys/ INSERT YOUR OWN API KEY"; - String line;
- StringBuilder builder = new StringBuilder();
- connection.getInputStream()));
- while ((line = reader.readLine()) != null) {
- builder.append(line);
- }
- Log.d("not good",builder.toString());
- //no, this is not yet a valid json response :)
- int start = builder.toString().indexOf("(") + 1;
- int end = builder.toString().length() - 1;
- //after cutting off the junk, its ok
- JSONObject jSONObject = new JSONObject(jSONString); //whole json object
- JSONObject jSONObjectInner = jSONObject.getJSONObject("photos"); //inner Json object
- JSONArray photoArray = jSONObjectInner.getJSONArray("photo"); // inner array of photos
- JSONObject photo = photoArray.getJSONObject((int) (limit*Math.random())); //get one random photo from array
- return constructFlickrImgUrl(photo, size._t);
- }
- // source: flickr.com/services/api/misc.u
rls.html - enum size {
- _s , _t ,_m
- };
- //helper method, to construct the url from the json object. You can define the size of the image that you want, with the size parameter. Be aware that not all images on flickr are available in all sizes.
- StringBuilder sb = new StringBuilder();
- sb.append("http://farm");
- sb.append(FARMID);
- sb.append(".static.flickr.com/"
;); - sb.append(SERVERID);
- sb.append("/");
- sb.append(ID);
- sb.append("_");
- sb.append(SECRET);
- sb.append(size.toString());
- sb.append(".jpg");
- return sb.toString();
- }
- /**
- * Word randomizer for fun
- * @param length
- * @return
- */
- char i[] = new char[length];
- for (int j = 0; j < length; j++) {
- }
- }
- //Usage:
- flickrApi(randomizer(3) , 1));
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
9 years 42 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
9 years 42 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
9 years 42 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
9 years 42 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
9 years 42 weeks ago
Poll
Useful resources
Android Development Projects
- Diseño de programas para Whatsapp para seguimiento y respuesta automática a Contactos
- expert needed to create android game IQ monopoly
- IOS and Android app
- i need a monile app developer android
- Need Mobile App for BigBlueButton
- ecommmerce
- Desarrollo de una APP para vender productos online
- Build a Market Place
- Build Me an App
- firebase configuration in Android.