Wednesday, April 17, 2013

Get all brands of cameras that Flickr knows about, using Flickr API.

Flickr Service provide API of flickr.cameras.getBrands, returns all the brands of cameras that Flickr knows about. It's a exercise to show how to access Flickr API using HttpClient(QueryFlickr() method), and parse the returned JSON(ParseJSON_getBrands() method).

Get all brands of cameras that Flickr knows about, using Flickr API.


MainActivity.java. You have to insert your own Flickr API Key in Flickr_KEY.
package com.example.androidflickrcameras;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

/*
* Example of Flickr API, flickr.cameras.getBrands
* http://api.flickr.com/services/rest/?method=flickr.cameras.getBrands&api_key=0532aeeb07bd212823a343a9194b8501&format=json&nojsoncallback=1
*/

final static String Flickr_KEY = "insert your Flickr API key here";
final static String Flickr_SECRET = "ba967558b9797793";
final static String subUrl_Flickr = "http://api.flickr.com/services/rest/?";
final static String subUrl_method_getBrands = "flickr.cameras.getBrands";
final static String url_FlickrApi_getBrands
= subUrl_Flickr
+ "method=" + subUrl_method_getBrands
+ "&api_key=" + Flickr_KEY
+ "&format=json&nojsoncallback=1";

TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.result);

new MyTask().execute();
}

private String QueryFlickr(String q){
String qResult = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(q);

try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();

String stringReadLine = null;

while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}

qResult = stringBuilder.toString();
}


} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return qResult;
}

private String ParseJSON_getBrands(String json){
String jResult = null;

try {
JSONObject JsonObject = new JSONObject(json);
JSONObject Json_brands = JsonObject.getJSONObject("brands");
JSONArray JsonArray_brand = Json_brands.getJSONArray("brand");

jResult = "";
for(int i = 0; i < JsonArray_brand.length(); i++){
JSONObject jsonObjectBrand = JsonArray_brand.getJSONObject(i);
jResult += "id: " + jsonObjectBrand.getString("id") + "\n"
+ "name: " + jsonObjectBrand.getString("name") +"\n\n";

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jResult;
}

private class MyTask extends AsyncTask<Void, Void, Void>{

String flickrReturn;
String parsedFlickrReturn;

@Override
protected Void doInBackground(Void... arg0) {
flickrReturn = QueryFlickr(url_FlickrApi_getBrands);
parsedFlickrReturn = ParseJSON_getBrands(flickrReturn);
return null;
}

@Override
protected void onPostExecute(Void result) {
if(parsedFlickrReturn == null){
tvResult.setText("Fail!");
}else{
tvResult.setText(parsedFlickrReturn);
}
}

}

}


Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>


</LinearLayout>


Permission of "android.permission.INTERNET" is needed.

download filesDownload the files.

Related:
- Get all the models for a given camera brand, using Flickr API.

No comments:

Post a Comment