Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561334
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Android平台

2013-04-03 10:33:52

本文记录阅读《Android多媒体开发高级编程——为智能手机和平板电脑开发图形、音乐、视频和富媒体应用》第十二章“使用web服务的媒体消费和发布”,通过调用Flickr的REST API,生成照片列表的应用。在线照片地址:
效果图如下:



我们需要在flickr上获取一个app secret。这里不过多描述。下面直接贴代码:

常量Constants.java中定义了API_KEY等信息

点击(此处)折叠或打开

  1. package flickr;

  2. public class Constants {

  3.     public static final String API_KEY = "465257bbc0011ba13bb6437c545bd6ec";
  4.     public static final String API_SECRET = "4a34e548c49499ca";
  5.     public static final String USER_ID = "94448382@N04";
  6.     public static final String REST_URL_ROOT = "";
  7.     public static final String PHOTO_URL_ROOT = "";
  8.     //29096781@N02/3606436456/
  9.     //user id(owner)/photo id/
  10. }

layout文件:包括主界面和每一行的布局定义。

主界面:包含一个ListView,如下photolist_main.xml:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.     xmlns:android=""
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     android:background="#FFFFFF">
  8.     
  9.      <ListView
  10.         android:id="@+id/photoListView"
  11.         android:layout_width="fill_parent"
  12.         android:layout_height="fill_parent" />
  13.      
  14. </LinearLayout>

行布局 photolist_item_row.xml 定义了每一行的布局,包含一个显示图片的ImageView和一个显示文本的TextView:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.     xmlns:android=""
  4.     android:orientation="horizontal"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     android:padding="10dp">
  8.     
  9.      <ImageView
  10.          android:id="@+id/photoView"
  11.         android:layout_width="64dp"
  12.         android:layout_height="64dp"
  13.         android:gravity="center_vertical"
  14.         android:layout_marginRight="15dp"
  15.         android:layout_marginTop="5dp"
  16.         android:layout_marginBottom="5dp"
  17.          />
  18.         
  19.      <TextView android:id="@+id/photoTitle"
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:gravity="center_vertical"
  23.         android:textStyle="bold"
  24.         android:textColor="#000000"
  25.         android:layout_marginTop="5dp"
  26.         android:layout_marginBottom="5dp" />
  27.         
  28. </LinearLayout>
通过Flickr响应的json数据抽象出来的数据模型FlickrPhoto.java

点击(此处)折叠或打开

  1. package flickr;

  2. public class FlickrPhoto {

  3.     private String id;
  4.     private String owner;
  5.     private String secret;
  6.     private String server;
  7.     private String title;
  8.     private String farm;
  9.     public String getId() {
  10.         return id;
  11.     }
  12.     public void setId(String id) {
  13.         this.id = id;
  14.     }
  15.     public String getOwner() {
  16.         return owner;
  17.     }
  18.     public void setOwner(String owner) {
  19.         this.owner = owner;
  20.     }
  21.     public String getSecret() {
  22.         return secret;
  23.     }
  24.     public void setSecret(String secret) {
  25.         this.secret = secret;
  26.     }
  27.     public String getServer() {
  28.         return server;
  29.     }
  30.     public void setServer(String server) {
  31.         this.server = server;
  32.     }
  33.     public String getTitle() {
  34.         return title;
  35.     }
  36.     public void setTitle(String title) {
  37.         this.title = title;
  38.     }
  39.     public String getFarm() {
  40.         return farm;
  41.     }
  42.     public void setFarm(String farm) {
  43.         this.farm = farm;
  44.     }
  45.     
  46.     public String makeURL(){
  47.         return ""+farm+".static.flickr.com/"+server+"/"+id+"_"+secret+"_m.jpg";
  48.     }

  49.     
  50. }

图片列表Activity对应的java类MmActivity.java

点击(此处)折叠或打开

  1. package flickr;

  2. import org.json.JSONArray;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;

  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.widget.ListView;

  9. import com.example.twiter.R;
  10. import com.github.kevinsawicki.http.HttpRequest;

  11. public class MmActivity extends Activity {
  12.     private static final String TAG = "MmActivity";

  13.     FlickrPhoto[] photos;

  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.photolist_main);
  18.         
  19.         String getPublicPhotos = Constants.REST_URL_ROOT + "&method=flickr.people.getPublicPhotos&api_key=" +
  20.         Constants.API_KEY + "&user_id=" +
  21.         Constants.USER_ID +"&format=json&per_page=500";
  22.         String response = HttpRequest.get(getPublicPhotos).body();
  23.         response = response.substring("jsonFlickrApi(".length(), response.length()-1);
  24.                
  25.         Log.d(TAG,"log..." + getPublicPhotos);
  26.         Log.d(TAG, response);
  27.         if(response!=null && !"".equals(response)){
  28.             try {
  29.                 JSONObject thedata = new JSONObject(response);
  30.                 
  31.                 JSONObject photoData = thedata.getJSONObject("photos");
  32.                 JSONArray photoArr = photoData.getJSONArray("photo");
  33.                 int len = photoArr.length();
  34.                 photos = new FlickrPhoto[len];
  35.                 
  36.                 for(int i=0; i<len; i++){
  37.                     FlickrPhoto tempPhoto = new FlickrPhoto();//这一句放在for前面导致严重错误
  38.                     JSONObject photoObj = photoArr.getJSONObject(i);
  39.                     tempPhoto.setId(photoObj.getString("id"));
  40.                     tempPhoto.setOwner(photoObj.getString("owner"));
  41.                     tempPhoto.setTitle(photoObj.getString("title"));
  42.                     tempPhoto.setFarm(photoObj.getString("farm"));
  43.                     tempPhoto.setSecret(photoObj.getString("secret"));
  44.                     tempPhoto.setServer(photoObj.getString("server"));
  45.                     Log.d(TAG,"PHOTO i:"+ i +" id:"+ photoObj.getString("id"));
  46.                     photos[i] = tempPhoto;
  47.                 }
  48.                 
  49.             } catch (JSONException e) {
  50.                 e.printStackTrace();
  51.                 Log.e(TAG, "响应的不是json格式,"+e.getMessage());
  52.             }
  53.             
  54.             ListView photoListView = (ListView)this.findViewById(R.id.photoListView);
  55.             photoListView.setAdapter(new FlickrGalleryAdapter(this,photos));
  56.         }
  57.         /*
  58.         &method=flickr.people.getPublicPhotos&api_key=4ef2fe2affcdd6e13218f5ddd0e2500d&user_id=29096781@N02&format=json
  59.      */
  60.     }
  61.     
  62.     
  63. }

为主界面ListView提供数据的Adapter FlickrGalleryAdapter.java

点击(此处)折叠或打开

  1. package flickr;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;

  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.BaseAdapter;
  16. import android.widget.ImageView;
  17. import android.widget.TextView;

  18. import com.example.twiter.R;

  19. public class FlickrGalleryAdapter extends BaseAdapter {
  20.     
  21.     private Context context;
  22.     private FlickrPhoto[] photos;
  23.     
  24.     LayoutInflater inflater;
  25.     
  26.     public FlickrGalleryAdapter(Context context, FlickrPhoto[] photos){
  27.         this.context = context;
  28.         this.photos = photos;
  29.     }

  30.     @Override
  31.     public int getCount() {
  32.         
  33.         return photos.length;
  34.     }

  35.     @Override
  36.     public Object getItem(int position) {
  37.         return photos[position];
  38.     }

  39.     @Override
  40.     public long getItemId(int position) {
  41.         return position;
  42.     }

  43.     @Override
  44.     public View getView(int position, View convertView, ViewGroup parent) {
  45.         /*LayoutInflater inflater = (LayoutInflater) context
  46.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  47.         View photoRow = inflater.inflate(R.layout.photolist_item_row, parent,false);
  48.         ImageView photoView = (ImageView)photoRow.findViewById(R.id.photoView);
  49.         TextView photoTitle = (TextView)photoRow.findViewById(R.id.photoTitle);
  50.         
  51.         String url = photos[position].makeURL();
  52.         Log.d("URL", "_______________"+url);
  53.         photoView.setImageBitmap(getImgFromURL(url));
  54.         
  55.         photoTitle.setText(photos[position].getTitle());
  56.         return photoRow;
  57.         */
  58.         
  59.         
  60.         View row = convertView;
  61.         PhotoHolder holder = null;

  62.         if (row == null) {
  63.             LayoutInflater inflater = ((Activity) context).getLayoutInflater();
  64.             //generate view from a row layout xml
  65.             row = inflater.inflate(R.layout.photolist_item_row, parent, false);

  66.             holder = new PhotoHolder();
  67.             holder.photoView = (ImageView) row.findViewById(R.id.photoView);
  68.             holder.photoTitle = (TextView) row.findViewById(R.id.photoTitle);

  69.             row.setTag(holder);
  70.         } else {
  71.             holder = (PhotoHolder) row.getTag();//the Object stored in this view as a tag
  72.         }

  73.         FlickrPhoto photo = photos[position];
  74.         holder.photoTitle.setText(photo.getTitle());
  75.         holder.photoView.setImageBitmap(getImgFromURL(photo.makeURL()));
  76.         
  77.         Log.d("POSITION", "_______________"+position);
  78.         Log.d("URL", "_______________"+photo.makeURL());
  79.         Log.d("photo.getTitle()", "_______________"+photo.getTitle());
  80.         

  81.         return row;
  82.     }
  83.     
  84.     public Bitmap getImgFromURL(String url){
  85.         Bitmap bitMapImg = null;
  86.         URL photoUrl = null;
  87.             try {
  88.                 photoUrl = new URL(url);
  89.                 HttpURLConnection httpConn = (HttpURLConnection) photoUrl.openConnection();
  90.                 httpConn.setDoInput(true);
  91.                 httpConn.connect();
  92.                 InputStream is = httpConn.getInputStream();
  93.                 bitMapImg = BitmapFactory.decodeStream(is);
  94.                 
  95.             } catch (MalformedURLException e) {
  96.                 
  97.                 bitMapImg = Bitmap.createBitmap(50,50,Bitmap.Config.ARGB_8888);
  98.             } catch (IOException e) {
  99.                 
  100.             }
  101.             return bitMapImg;
  102.     }
  103.     
  104.     static class PhotoHolder {
  105.         ImageView photoView;
  106.         TextView photoTitle;
  107.     }
  108.     
  109.     
  110. }

服务端响应的json数据:

点击(此处)折叠或打开

  1. {"photos":{"page":1, "pages":1, "perpage":500, "total":"11", "photo":[{"id":"8614366579", "owner":"94448382@N04", "secret":"18f9546601", "server":"8402", "farm":9, "title":"MOVIE", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8615474674", "owner":"94448382@N04", "secret":"befe943159", "server":"8126", "farm":9, "title":"xiaofeixing4", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8614366639", "owner":"94448382@N04", "secret":"6b5000040b", "server":"8102", "farm":9, "title":"xiaofeixing3", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8615474714", "owner":"94448382@N04", "secret":"ce0abaee44", "server":"8530", "farm":9, "title":"xiaofeixing2", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8614366749", "owner":"94448382@N04", "secret":"4e1179ccf0", "server":"8398", "farm":9, "title":"xiaofeixing1", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8615474746", "owner":"94448382@N04", "secret":"41f15d9df5", "server":"8541", "farm":9, "title":"xiaofeixing", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8615474782", "owner":"94448382@N04", "secret":"656bdb2cb4", "server":"8395", "farm":9, "title":"movie2", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8615474790", "owner":"94448382@N04", "secret":"9aa25ecd9c", "server":"8101", "farm":9, "title":"movie1", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8614366763", "owner":"94448382@N04", "secret":"f03c1d90e7", "server":"8528", "farm":9, "title":"movie", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8597609376", "owner":"94448382@N04", "secret":"259e77d927", "server":"8230", "farm":9, "title":"weather", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8597609382", "owner":"94448382@N04", "secret":"06a2e4d618", "server":"8383", "farm":9, "title":"list0", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}

依赖项:
json/gson
com.github.kevinsawicki.http.HttpRequest




阅读(1951) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~