package com.jasonex1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class JsonEx extends Activity {
String apikey1="733a667ca5dd461af38f81e7b1d0e7e8";
FlickrPhoto[] photos;
int [] ph={R.drawable.icon,R.drawable.icon};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient httpclient=new DefaultHttpClient();
HttpGet httpget=new HttpGet("http://api.flickr.com/services/rest/?method=flickr.photos.search&tags=yourTag&format=json&api_key="+ apikey1 + "&per_page=5&nojsoncallback=1");
HttpResponse response;
try
{
response=httpclient.execute(httpget);
HttpEntity entity=response.getEntity();
if(entity!=null)
{
InputStream in=entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String currentline="";
try
{
while((currentline=br.readLine())!=null)
{
Log.d("line","current line data is"+currentline);
sb.append(currentline+"\n");
}
}catch(IOException e)
{
e.printStackTrace();
}
String result=sb.toString();
Log.d("result","result is"+result);
JSONObject thedata=new JSONObject(result);
JSONObject thephotosdata=thedata.getJSONObject("photos");
JSONArray thephotodata=thephotosdata.getJSONArray("photo");
photos=new FlickrPhoto[thephotodata.length()];
for(int i=0;i<thephotodata.length();i++)
{
JSONObject photodata=thephotodata.getJSONObject(i);
photos[i]=new FlickrPhoto(photodata.getString("id"),
photodata.getString("owner"),photodata.getString("secret"),photodata.getString("server"),
photodata.getString("title"),photodata.getString("farm"));
}
in.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setAdapter(new FlickrEx(this,photos));
}
public class FlickrEx extends BaseAdapter
{
Context con;
private FlickrPhoto[] photos;
LayoutInflater inflater;
FlickrEx(Context c,FlickrPhoto[] items)
{
this.con=c;
this.photos=items;
inflater=(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return photos.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View photorow=inflater.inflate(R.layout.row,null);
ImageView img=(ImageView)photorow.findViewById(R.id.ImageView);
img.setImageBitmap(imageFromUrl(photos[position].makeURL()));
TextView tv=(TextView)photorow.findViewById(R.id.TextView);
tv.setText("image");
return photorow;
}
public Bitmap imageFromUrl(String url)
{
Bitmap bitmap;
URL imageurl=null;
try
{
imageurl=new URL(url);
}catch(Exception e)
{
e.printStackTrace();
}
try
{
HttpURLConnection httpconnection=(HttpURLConnection)imageurl.openConnection();
httpconnection.setDoInput(true);
httpconnection.connect();
InputStream in=httpconnection.getInputStream();
bitmap=BitmapFactory.decodeStream(in);
}catch(Exception e)
{
e.printStackTrace();
bitmap=Bitmap.createBitmap(10,10,Bitmap.Config.ARGB_8888);
}
return bitmap;
}
}
class FlickrPhoto
{
String id;
String owner;
String secret;
String server;
String title;
String farm;
public FlickrPhoto(String id1,String owner1,String secret1,String server1,String title1,String farm1)
{
this.id=id1;
this.owner=owner1;
this.secret=secret1;
this.server=server1;
this.farm=farm1;
this.title=title1;
}
public String makeURL()
{
return "http://farm" + farm + ".static.flickr.com/" + server + "/"+ id + "_" + secret + "_m.jpg";
}
}
}
No comments:
Post a Comment