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

Flickr Service provide API of flickr.cameras.getBrandModels, returns all the models for a given camera brand. It's a exercise to show how to access get models of brand "nikon".

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


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.getBrandModels
* http://api.flickr.com/services/rest/?method=flickr.cameras.getBrandModels&api_key=65d0c67c148bb772766f317c5118f380&brand=Nikon&format=json&nojsoncallback=1
*/

final static String Flickr_KEY = "insert your Flickr API key here";
final static String subUrl_Flickr = "http://api.flickr.com/services/rest/?";
final static String subUrl_method_getBrandModels = "flickr.cameras.getBrandModels";
final static String brand = "nikon";
final static String url_FlickrApi_getBrandModels
= subUrl_Flickr
+ "method=" + subUrl_method_getBrandModels
+ "&api_key=" + Flickr_KEY
+ "&brand=" + brand
+ "&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_getBrandModels(String json){
String jResult = null;

try {
JSONObject JsonObject = new JSONObject(json);
JSONObject Json_cameras = JsonObject.getJSONObject("cameras");

JSONArray JsonArray_camera = Json_cameras.getJSONArray("camera");

jResult = "Brand: " + Json_cameras.getString("brand") + "\n\n";
for(int i = 0; i < JsonArray_camera.length(); i++){
JSONObject jsonObjectCamera = JsonArray_camera.getJSONObject(i);

String camera_id = jsonObjectCamera.getString("id");

JSONObject jsonObjectCamera_name = jsonObjectCamera.getJSONObject("name");
String camera_name_content = jsonObjectCamera_name.getString("_content");

JSONObject jsonObjectCamera_details = jsonObjectCamera.getJSONObject("details");
String details_megapixels
= jsonObjectCamera_details
.getJSONObject("megapixels").getString("_content");
String details_lcd_screen_size
= jsonObjectCamera_details
.getJSONObject("lcd_screen_size").getString("_content");
String details_memory_type
= jsonObjectCamera_details
.getJSONObject("memory_type").getString("_content");

jResult += "id: " + camera_id + "\n"
+ "name: " + camera_name_content + "\n"
+ "megapixels: " + details_megapixels + "\n"
+ "lcd_screen_size: " + details_lcd_screen_size + "\n"
+ "memory_type: " + details_memory_type + "\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_getBrandModels);
parsedFlickrReturn = ParseJSON_getBrandModels(flickrReturn);
return null;
}

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

}

}


Layout refer to the exercise "Get all brands of cameras that Flickr knows about, using Flickr API".

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


download filesDownload the files.