当前位置: 首页 > news >正文

东莞网页制作价格/seo资源

东莞网页制作价格,seo资源,软件商店安装下载,宁波seo排名方案优化2013年11月23日 新浪微博客户端系列博客记录 之前获取首页微博列表还没有介绍adapter,关于adapter可以分出来一块来讲,用过ListView的童鞋们肯定对adapter不会陌生,下面是Android提供的一些Adapter,适用与一些简单的数据填充。 B…


2013年11月23日  新浪微博客户端系列博客记录

之前获取首页微博列表还没有介绍adapter,关于adapter可以分出来一块来讲,用过ListView的童鞋们肯定对adapter不会陌生,下面是Android提供的一些Adapter,适用与一些简单的数据填充。

  • BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
  • ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
  • SimpleAdapter有最好的扩充性,可以自定义出各种效果。
  • SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

到了新浪微博这里就需要自定义Adapter了,使用Android提供的adapter已经不能满足需求了,目前小巫所开发的有以下自定义adapter:

AppsListAdaper:用于显示联想搜索得到的app列表。
CommentListAdapter:用于显示评论列表。
FaceListAdapter:用于显示表情列表。
MSGAdapter:用于显示消息页面列表。
UsersListAdaper:用于显示联想搜索得到的User列表。
WeiboListAdapter:用于显示微博列表(首页、收藏、@我的)。
WeiboListNoMoreAdapter:在WeboListAdapter基础上去掉“更多”的adapter。


首页界面效果:


今天我只介绍WeiboListAdapter

/xiaowu_twitter/src/com/wwj/sina/weibo/adapters/WeiboListAdapter.java


WeiboListAdapter是稍显复杂的,因为它要显示的数据比较多,涉及到微博图片的显示,转发内容的显示,先来看看代码先吧。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.wwj.sina.weibo.adapters;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.net.Uri;  
  11. import android.text.Html;  
  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.LinearLayout;  
  18. import android.widget.TextView;  
  19.   
  20. import com.wwj.sina.weibo.R;  
  21. import com.wwj.sina.weibo.interfaces.Const;  
  22. import com.wwj.sina.weibo.library.StorageManager;  
  23. import com.wwj.sina.weibo.library.WeiboManager;  
  24. import com.wwj.sina.weibo.object.Status;  
  25. import com.wwj.sina.weibo.util.Tools;  
  26. import com.wwj.sina.weibo.workqueue.DoneAndProcess;  
  27. import com.wwj.sina.weibo.workqueue.task.ParentTask;  
  28.   
  29. /** 
  30.  * 微博列表适配器 主要用来显示首页、提示我的、收藏微博列表 
  31.  *  
  32.  * @author wwj 
  33.  *  
  34.  */  
  35. public class WeiboListAdapter extends BaseAdapter implements Const,  
  36.         DoneAndProcess {  
  37.     protected Activity activity;  
  38.     protected int type;  
  39.     protected LayoutInflater layoutInflater;  
  40.     protected List<Status> statuses;  
  41.   
  42.     public WeiboListAdapter(Activity activity) {  
  43.         this.activity = activity;  
  44.     }  
  45.   
  46.     /** 
  47.      * 需要传入一个已经封装微博数据的List对象 type表示 
  48.      *  
  49.      * @param activity 
  50.      * @param statuses 
  51.      * @param type 
  52.      *            表示处理的微博数据类型,在Const中定义相关常量 
  53.      */  
  54.     public WeiboListAdapter(Activity activity, List<Status> statuses, int type) {  
  55.         this.activity = activity;  
  56.         this.type = type;  
  57.         layoutInflater = (LayoutInflater) activity  
  58.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  59.         this.statuses = new ArrayList<Status>();  
  60.         if (statuses != null)  
  61.             this.statuses.addAll(statuses);  
  62.         try {  
  63.             // 保存微博数据  
  64.             StorageManager.saveList(statuses, PATH_STORAGE, type);  
  65.         } catch (Exception e) {  
  66.             e.printStackTrace();  
  67.         }  
  68.     }  
  69.   
  70.     @Override  
  71.     public int getCount() {  
  72.         // 获取微博数,加1为了最后一项显示"更多"  
  73.         return statuses.size() + 1;  
  74.     }  
  75.   
  76.     @Override  
  77.     public Object getItem(int position) {  
  78.         return null;  
  79.     }  
  80.   
  81.     @Override  
  82.     public long getItemId(int position) {  
  83.         return 0;  
  84.     }  
  85.   
  86.     // 获取制定位置的Status对象  
  87.     public Status getStatus(int position) {  
  88.         if (statuses.size() > 0) {  
  89.             return statuses.get(position);  
  90.         } else {  
  91.             return null;  
  92.         }  
  93.     }  
  94.   
  95.     // 获取已经显示的微博的最小ID  
  96.     public long getMinId() {  
  97.         if (statuses.size() > 0)  
  98.             return statuses.get(statuses.size() - 1).id;  
  99.         else  
  100.             return Long.MAX_VALUE;  
  101.     }  
  102.   
  103.     // 获取已经显示的微博最大ID  
  104.     public long getMaxId() {  
  105.         if (statuses.size() > 0)  
  106.             return statuses.get(0).id;  
  107.         else  
  108.             return 0;  
  109.     }  
  110.   
  111.     // 添加新的微博  
  112.     // 在刷新、显示更多微博时会根据不同的情况决定如何添加微博  
  113.     public void putStatuses(List<Status> statuses) {  
  114.         if (statuses == null || this.statuses == null)  
  115.             return;  
  116.         if (statuses.size() == 0)  
  117.             return;  
  118.         if (this.statuses.size() == 0) {  
  119.             this.statuses.addAll(statuses);  
  120.   
  121.         } else if (statuses  
  122.                 .get(0)  
  123.                 .getCreatedAt()  
  124.                 .before(this.statuses.get(this.statuses.size() - 1)  
  125.                         .getCreatedAt())) {  
  126.             this.statuses.addAll(statuses);  
  127.             // 添加的statuses比原来的新,并且数量小于等于默认返回数量,直接将statuses添加到前面  
  128.         } else if (statuses.get(statuses.size() - 1).getCreatedAt()  
  129.                 .after(this.statuses.get(0).getCreatedAt())  
  130.                 && statuses.size() <= DEFAULT_STATUS_COUNT) {  
  131.             this.statuses.addAll(0, statuses);  
  132.         } else {  
  133.             this.statuses.clear();  
  134.             this.statuses.addAll(statuses);  
  135.         }  
  136.         try {  
  137.             // 保存微博信息  
  138.             StorageManager.saveList(this.statuses, PATH_STORAGE, type);  
  139.         } catch (Exception e) {  
  140.             e.printStackTrace();  
  141.         }  
  142.         // 并且刷新ListView  
  143.         this.notifyDataSetChanged();  
  144.     }  
  145.   
  146.     // 控制View行为的  
  147.     private boolean showMoreAnimFlag = false;  
  148.     protected boolean showRefreshAnimFlag = false;  
  149.   
  150.     public void showMoreAnim() {  
  151.         showMoreAnimFlag = true;  
  152.         notifyDataSetChanged();  
  153.     }  
  154.   
  155.     public void hideMoreAnim() {  
  156.         showMoreAnimFlag = false;  
  157.         notifyDataSetChanged();  
  158.     }  
  159.   
  160.     public void showRefreshAnim() {  
  161.         showMoreAnimFlag = true;  
  162.         notifyDataSetChanged();  
  163.     }  
  164.   
  165.     public void hideRefreshAnim() {  
  166.         showRefreshAnimFlag = false;  
  167.         notifyDataSetChanged();  
  168.     }  
  169.   
  170.     // 通过url装载要显示的图像,如果图像文件不存在,回通过hideView标志决定是否隐藏ImageView组件  
  171.     // hideView: true 隐藏ImageView hideView:false 不做任何动作  
  172.     private void loadImage(ImageView imageView, String url, boolean hideView) {  
  173.         if (url != null) {  
  174.             String imageUrl = WeiboManager.getImageurl(activity, url);  
  175.             if (imageUrl != null) {  
  176.                 imageView.setImageURI(Uri.fromFile(new File(imageUrl)));  
  177.                 imageView.setVisibility(View.VISIBLE);  
  178.                 return;  
  179.             }  
  180.         }  
  181.         if (hideView)  
  182.             imageView.setVisibility(View.GONE);  
  183.     }  
  184.   
  185.     @Override  
  186.     public View getView(int position, View convertView, ViewGroup parent) {  
  187.         if (convertView == null) {  
  188.             convertView = layoutInflater  
  189.                     .inflate(R.layout.weibo_list_item, null);  
  190.         }  
  191.   
  192.         View weiboListItem = convertView.findViewById(R.id.ll_weibo_list_item); // 微博列表项  
  193.         View moreWeiboListItem = convertView  
  194.                 .findViewById(R.id.rl_more_weibo_list_item); // “更多”列表项  
  195.         View refreshWeiboListItem = convertView  
  196.                 .findViewById(R.id.rl_refresh_weibo_list_item); // "刷新"列表项  
  197.         refreshWeiboListItem.setVisibility(View.GONE);  
  198.         // 当列表项不是最后一项时继续执行  
  199.         if (position < statuses.size()) {  
  200.             weiboListItem.setVisibility(View.VISIBLE);  
  201.             moreWeiboListItem.setVisibility(View.GONE); // “更多”不显示  
  202.             Status status = statuses.get(position);  
  203.   
  204.             TextView statusText = (TextView) convertView  
  205.                     .findViewById(R.id.tv_text);  
  206.             TextView name = (TextView) convertView.findViewById(R.id.tv_name);  
  207.             TextView createAt = (TextView) convertView  
  208.                     .findViewById(R.id.tv_created_at);  
  209.             ImageView profileImage = (ImageView) convertView  
  210.                     .findViewById(R.id.iv_profile_image);  
  211.             profileImage.setImageResource(R.drawable.portrait); // 设置默认头像  
  212.             ImageView picture = (ImageView) convertView  
  213.                     .findViewById(R.id.iv_picture);  
  214.             ImageView statusImage = (ImageView) convertView  
  215.                     .findViewById(R.id.iv_status_image);  
  216.             ImageView verified = (ImageView) convertView  
  217.                     .findViewById(R.id.iv_verified);  
  218.   
  219.             verified.setVisibility(View.GONE); // 设置认证不可见  
  220.   
  221.             if (status.user != null) {  
  222.                 // 设置用户认证图标,即各种颜色的“V”或其他符号  
  223.                 Tools.userVerified(verified, status.user.verified_type);  
  224.             }  
  225.   
  226.             statusImage.setImageBitmap(null);  
  227.             LinearLayout insideContent = (LinearLayout) convertView  
  228.                     .findViewById(R.id.ll_inside_content);  
  229.             ImageView retweetdetailImage = (ImageView) convertView  
  230.                     .findViewById(R.id.iv_retweetdetail_image);  
  231.             retweetdetailImage.setImageBitmap(null);  
  232.             TextView retweetdetailText = (TextView) convertView  
  233.                     .findViewById(R.id.tv_retweetdetail_text);  
  234.             TextView source = (TextView) convertView  
  235.                     .findViewById(R.id.tv_source);  
  236.   
  237.             // 装载图像  
  238.             if (status.user != null) {  
  239.                 loadImage(profileImage, status.user.profile_image_url, false);  
  240.             }  
  241.             loadImage(statusImage, status.thumbnail_pic, true);  
  242.   
  243.             statusText.setText(Tools.changeTextToFace(activity,  
  244.                     Html.fromHtml(Tools.atBlue(status.text))));  
  245.   
  246.             if (status.user != null)  
  247.                 name.setText(status.user.name); // 显示用户昵称  
  248.             // 当前微博有图像  
  249.             if (WeiboManager.hasPicture(status))  
  250.                 picture.setVisibility(View.VISIBLE);  
  251.             else  
  252.                 picture.setVisibility(View.INVISIBLE);  
  253.   
  254.             createAt.setText(Tools.getTimeStr(status.getCreatedAt(), new Date()));  
  255.             source.setText("来自: " + status.getTextSource()); // 设置微博来源  
  256.             if (status.retweeted_status != null // 如果转发的数据不为空  
  257.                     && status.retweeted_status.user != null) {  
  258.                 insideContent.setVisibility(View.VISIBLE);  
  259.   
  260.                 retweetdetailText.setText(Html.fromHtml(Tools.atBlue("@"  
  261.                         + status.retweeted_status.user.name + ":"  
  262.                         + status.retweeted_status.text)));  
  263.                 loadImage(retweetdetailImage,  
  264.                         status.retweeted_status.thumbnail_pic, false);  
  265.             } else {  
  266.                 insideContent.setVisibility(View.GONE);  
  267.             }  
  268.   
  269.         } else {  
  270.             weiboListItem.setVisibility(View.GONE);  
  271.             moreWeiboListItem.setVisibility(View.VISIBLE); // 显示“更多”  
  272.             View moreAnim = convertView.findViewById(R.id.pb_more);  
  273.   
  274.             if (showMoreAnimFlag) {  
  275.                 moreAnim.setVisibility(View.VISIBLE);  
  276.             } else {  
  277.                 moreAnim.setVisibility(View.GONE);  
  278.             }  
  279.         }  
  280.         return convertView;  
  281.     }  
  282.   
  283.     @Override  
  284.     public void doneProcess(ParentTask task) {  
  285.         notifyDataSetChanged(); // 通知更新列表数据  
  286.     }  
  287. }  

关于adapter我们需要关注getView这个方法,因为我们要显示的内容都在这里完成,由于每一条的微博数据比较多,所以童鞋们要注意每一项内容显示到哪个控件上。显示的数据是通过构造方法传进来的,每一条微博数据就是一个Status对象,所以我们需要定义相应的实体类,这个类我们怎么知道怎么定义,这就要知道请求API所返回的内容,

比如:https://api.weibo.com/2/statuses/home_timeline.json。我们请求这个API的时候,返回的字段就是我们需要定义的,如果不太清楚,既要好好看看一下开放平台给我们说明。

可以到这里查看:http://open.weibo.com/wiki/2/statuses/home_timeline。

那里很清楚说明了,请求方式,请求参数还有返回的json数据结构。

返回字段说明

返回值字段 字段类型 字段说明
created_at string 微博创建时间
id int64 微博ID
mid int64 微博MID
idstr string 字符串型的微博ID
text string 微博信息内容
source string 微博来源
favorited boolean 是否已收藏,true:是,false:否
truncated boolean 是否被截断,true:是,false:否
in_reply_to_status_id string (暂未支持)回复ID
in_reply_to_user_id string (暂未支持)回复人UID
in_reply_to_screen_name string (暂未支持)回复人昵称
thumbnail_pic string 缩略图片地址,没有时不返回此字段
bmiddle_pic string 中等尺寸图片地址,没有时不返回此字段
original_pic string 原始图片地址,没有时不返回此字段
geo object 地理信息字段 详细
user object 微博作者的用户信息字段 详细
retweeted_status object 被转发的原微博信息字段,当该微博为转发微博时返回 详细
reposts_count int 转发数
comments_count int 评论数
attitudes_count int 表态数
mlevel int 暂未支持
visible object 微博的可见性及指定可见分组信息。该object中type取值,0:普通微博,1:私密微博,3:指定分组微博,4:密友微博;list_id为分组的组号
pic_urls object 微博配图地址。多图时返回多图链接。无配图返回“[]”
ad object array 微博流内的推广微博ID


[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.wwj.sina.weibo.object;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import com.wwj.sina.weibo.interfaces.WeiboObject;  
  7. import com.wwj.sina.weibo.util.Tools;  
  8.   
  9. import android.text.Html;  
  10. /** 
  11.  * 微博实体类 
  12.  * @author wwj 
  13.  * 
  14.  */  
  15. @SuppressWarnings("serial")  
  16. public class Status implements WeiboObject, Serializable {  
  17.     /** 字符串型的微博ID */  
  18.     public String idstr;  
  19.     /** 创建时间 */  
  20.     public String created_at;  
  21.     /** 微博ID */  
  22.     public long id;  
  23.     /** 微博信息内容 */  
  24.     public String text;  
  25.     /** 微博来源(html形式) */  
  26.     public String source;  
  27.   
  28.     /** 是否已收藏 */  
  29.     public boolean favorited;  
  30.     /** 是否被截断 */  
  31.     public boolean truncated;  
  32.     /** 回复ID */  
  33.     public long in_reply_to_status_id;  
  34.     /** 回复人UID */  
  35.     public long in_reply_to_user_id;  
  36.     /** 回复人昵称 */  
  37.     public String in_reply_to_screen_name;  
  38.     /** 微博MID */  
  39.     public long mid;  
  40.     /** 中等尺寸图片地址 */  
  41.     public String bmiddle_pic;  
  42.     /** 原始图片地址 */  
  43.     public String original_pic;  
  44.     /** 缩略图片地址 */  
  45.     public String thumbnail_pic;  
  46.     /** 转发数 */  
  47.     public int reposts_count;  
  48.     /** 评论数 */  
  49.     public int comments_count;  
  50.     /** 转发的微博内容 */  
  51.     public Status retweeted_status;  
  52.     /** 微博作者的用户信息字段 */  
  53.     public User user; // 不要初始化,否则可能会引起递归创建对象,导致stack溢出  
  54.   
  55.     /** 获取Date形式的创建时间 */  
  56.     public Date getCreatedAt() {  
  57.         return Tools.strToDate(created_at);  
  58.     }  
  59.   
  60.     /** 文本形式的source, */  
  61.     private String text_source;  
  62.   
  63.     /** 获取文本形式的source */  
  64.     public String getTextSource() {  
  65.         if (text_source == null) {  
  66.             try {  
  67.                 // 有时返回的来源是null,可能是一个bug,所以必须加上try...catch...  
  68.                 text_source = Html.fromHtml(source).toString();  
  69.             } catch (Exception e) {  
  70.                 text_source = source;  
  71.             }  
  72.   
  73.         }  
  74.   
  75.         return text_source;  
  76.   
  77.     }  
  78. }  

我们在授权第三方应用程序访问用户的数据成功后,就会异步获取用户首页微博数据,然后显示到界面上。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.wwj.sina.weibo.listener;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Message;  
  9. import android.view.View;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.Toast;  
  12.   
  13. import com.weibo.net.DialogError;  
  14. import com.weibo.net.WeiboDialogListener;  
  15. import com.weibo.net.WeiboException;  
  16. import com.weibo.net.AsyncWeiboRunner.RequestListener;  
  17. import com.wwj.sina.weibo.Home;  
  18. import com.wwj.sina.weibo.R;  
  19. import com.wwj.sina.weibo.adapters.WeiboListAdapter;  
  20. import com.wwj.sina.weibo.interfaces.Const;  
  21. import com.wwj.sina.weibo.library.JSONAndObject;  
  22. import com.wwj.sina.weibo.library.StorageManager;  
  23. import com.wwj.sina.weibo.library.WeiboManager;  
  24. import com.wwj.sina.weibo.listener.impl.StatusRequestListenerImpl;  
  25. import com.wwj.sina.weibo.object.Status;  
  26. import com.wwj.sina.weibo.object.User;  
  27. import com.wwj.sina.weibo.util.LogUtils;  
  28. import com.wwj.sina.weibo.util.SettingUtil;  
  29. import com.wwj.sina.weibo.util.Tools;  
  30.   
  31. public class AuthDialogListener implements WeiboDialogListener, Const {  
  32.     private Activity activity;  
  33.   
  34.     public AuthDialogListener(Activity activity) {  
  35.         super();  
  36.         this.activity = activity;  
  37.     }  
  38.   
  39.     // 认证成功后调用  
  40.     public void onComplete(Bundle values) {  
  41.         // 保存access_token 和 expires_in  
  42.         String token = values.getString("access_token");  
  43.         String expires_in = values.getString("expires_in");  
  44.         SettingUtil.set(activity, SettingUtil.ACCESS_TOKEN, token);  
  45.         SettingUtil.set(activity, SettingUtil.EXPIRES_IN, expires_in);  
  46.         Toast.makeText(activity, "认证成功", Toast.LENGTH_SHORT).show();  
  47.   
  48.         final Home homeActivity = (Home) activity;  
  49.         WeiboListAdapter weiboListAdapter = null;  
  50.         // 得到用户的唯一标识  
  51.         long uid = Long.parseLong(values.getString("uid"));  
  52.         // 保存用户UID  
  53.         StorageManager.setValue(activity, "uid", uid);  
  54.   
  55.         WeiboManager.getUserAsync(homeActivity,  
  56.                 StorageManager.getValue(homeActivity, "uid"0),  
  57.                 new RequestListener() {  
  58.   
  59.                     @Override  
  60.                     public void onIOException(IOException e) {  
  61.                     }  
  62.   
  63.                     @Override  
  64.                     public void onError(WeiboException e) {  
  65.                     }  
  66.   
  67.                     @Override  
  68.                     public void onComplete(String response) {  
  69.                         User user = new User();  
  70.                         JSONAndObject.convertSingleObject((Object) user,  
  71.                                 response);  
  72.                         Message msg = new Message();  
  73.                         msg.obj = user;  
  74.                         homeActivity.handler.sendMessage(msg);  
  75.                     }  
  76.                 });  
  77.   
  78.         View homeReloadAnim = homeActivity.findViewById(R.id.pb_home_reload);  
  79.         View homeReload = homeActivity.findViewById(R.id.btn_home_reload);  
  80.         homeReloadAnim.setVisibility(View.VISIBLE);  
  81.         homeReload.setVisibility(View.GONE);  
  82.   
  83.         LinearLayout ll_home_layout = (LinearLayout) homeActivity  
  84.                 .findViewById(R.id.ll_home_layout);  
  85.         List<Status> statuses = WeiboManager.getHomeTimelineAsync(homeActivity,  
  86.                 new StatusRequestListenerImpl(homeActivity, ll_home_layout,  
  87.                         HOME));  
  88.         weiboListAdapter = new WeiboListAdapter(activity, statuses, Const.HOME);  
  89.   
  90.         homeActivity.homeTimelineAdapter = weiboListAdapter;  
  91.         homeActivity.imageWorkQueueMonitor = Tools  
  92.                 .getGlobalObject(homeActivity).getImageWorkQueueMonitor(  
  93.                         homeActivity);  
  94.         homeActivity.taskWorkQueueMonitor = Tools.getGlobalObject(homeActivity)  
  95.                 .getTaskWorkQueueMonitor(activity);  
  96.         homeActivity.imageWorkQueueMonitor.addDoneAndProcess(Const.HOME,  
  97.                 weiboListAdapter);  
  98.     }  
  99.   
  100.     public void onWeiboException(WeiboException e) {  
  101.         LogUtils.e("### onWeiboException");  
  102.         // 当认证过程中捕获到WeiboException时调用  
  103.         Toast.makeText(activity, "Auth exception:" + e.getMessage(),  
  104.                 Toast.LENGTH_LONG).show();  
  105.         activity.finish();  
  106.     }  
  107.   
  108.     public void onError(DialogError e) {  
  109.         LogUtils.e("### onError");  
  110.         // Oauth2.0认证过程中,当认证对话框中的webView接收数据出现错误时调用此方法  
  111.         Toast.makeText(activity, "Auth error:" + e.getMessage(),  
  112.                 Toast.LENGTH_LONG).show();  
  113.         activity.finish();  
  114.     }  
  115.   
  116.     public void onCancel() {  
  117.         LogUtils.e("### onCancel");  
  118.         // Oauth2.0认证过程中,如果认证窗口被关闭或认证取消时调用  
  119.         Toast.makeText(activity, "Auth cancel", Toast.LENGTH_LONG).show();  
  120.         activity.finish();  
  121.     }  
  122.   
  123. }  


在认证成功后,回调onComplete方法,就会获取用户个人信息,显示到标题上。下面的代码我解释一下:

statuses的数据是由WeiboManager类当中getHomeTimelineAsync返回来的,这方法传入了一个监听器,StatusRequestListenerImpl这里类实现AsyncWeiboRunner类的RequestListener接口。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. LinearLayout ll_home_layout = (LinearLayout) homeActivity  
  2.                 .findViewById(R.id.ll_home_layout);  
  3.         List<Status> statuses = WeiboManager.getHomeTimelineAsync(homeActivity,  
  4.                 new StatusRequestListenerImpl(homeActivity, ll_home_layout,  
  5.                         HOME));  
  6.         weiboListAdapter = new WeiboListAdapter(activity, statuses, Const.HOME);  
  7.   
  8.         homeActivity.homeTimelineAdapter = weiboListAdapter;  
  9.         homeActivity.imageWorkQueueMonitor = Tools  
  10.                 .getGlobalObject(homeActivity).getImageWorkQueueMonitor(  
  11.                         homeActivity);  
  12.         homeActivity.taskWorkQueueMonitor = Tools.getGlobalObject(homeActivity)  
  13.                 .getTaskWorkQueueMonitor(activity);  
  14.         homeActivity.imageWorkQueueMonitor.addDoneAndProcess(Const.HOME,  
  15.                 weiboListAdapter);  


AsyncWeiboRunner这个就是实现异步请求数据的类,通过构造函数传入进来的listener,调用它的onComplete方法就可以把请求的数据得到。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Copyright 2011 Sina. 
  3.  * 
  4.  * Licensed under the Apache License and Weibo License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *    http://www.open.weibo.com 
  9.  *    http://www.apache.org/licenses/LICENSE-2.0 
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software 
  12.  * distributed under the License is distributed on an "AS IS" BASIS, 
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14.  * See the License for the specific language governing permissions and 
  15.  * limitations under the License. 
  16.  */  
  17. package com.weibo.net;  
  18.   
  19. import java.io.IOException;  
  20.   
  21. import com.wwj.sina.weibo.util.LogUtils;  
  22.   
  23. import android.content.Context;  
  24.   
  25. /** 
  26.  * Encapsulation main Weibo APIs, Include: 1. getRquestToken , 2. 
  27.  * getAccessToken, 3. url request. Implements a weibo api as a asynchronized 
  28.  * way. Every object used this runner should implement interface 
  29.  * RequestListener. 
  30.  * 异步请求数据的类 
  31.  * @author ZhangJie (zhangjie2@staff.sina.com.cn) 
  32.  */  
  33. public class AsyncWeiboRunner {  
  34.   
  35.     private Weibo mWeibo;  
  36.   
  37.     public AsyncWeiboRunner(Weibo weibo) {  
  38.         this.mWeibo = weibo;  
  39.     }  
  40.   
  41.     public void request(final Context context, final String url,  
  42.             final WeiboParameters params, final String httpMethod,  
  43.             final RequestListener listener) {  
  44.         LogUtils.d("### AsyncWeiboRunner request");  
  45.         new Thread() {  
  46.             @Override  
  47.             public void run() {  
  48.                 try {  
  49.                       
  50.                     String resp = mWeibo.request(context, url, params,  
  51.                             httpMethod, mWeibo.getAccessToken());  
  52.   
  53.                     listener.onComplete(resp);  
  54.                 } catch (WeiboException e) {  
  55.                     listener.onError(e);  
  56.                 }  
  57.             }  
  58.         }.start();  
  59.   
  60.     }  
  61.   
  62.     // 请求接口  
  63.     public static interface RequestListener {  
  64.   
  65.         public void onComplete(String response);  
  66.   
  67.         public void onIOException(IOException e);  
  68.   
  69.         public void onError(WeiboException e);  
  70.   
  71.     }  
  72.   
  73. }  


所以真正显示数据的地方在StatusRequestListenerImpl里,通过回调onComplete方法得到的respone,respone就是请求api返回的json数据,我们通过JSONAndObject的convert方法,将json字符串转换为以status作为元素的List。之后就是通过handler发送消息,在界面显示数据了,具体细节你们需要好好看看代码。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.wwj.sina.weibo.listener.impl;  
  2.   
  3. import java.io.IOException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12.   
  13. import com.weibo.net.AsyncWeiboRunner.RequestListener;  
  14. import com.weibo.net.WeiboException;  
  15. import com.wwj.sina.weibo.Home;  
  16. import com.wwj.sina.weibo.MessageViewer;  
  17. import com.wwj.sina.weibo.R;  
  18. import com.wwj.sina.weibo.adapters.WeiboListAdapter;  
  19. import com.wwj.sina.weibo.adapters.WeiboListNoMoreAdapter;  
  20. import com.wwj.sina.weibo.interfaces.Const;  
  21. import com.wwj.sina.weibo.library.JSONAndObject;  
  22. import com.wwj.sina.weibo.library.WeiboManager;  
  23. import com.wwj.sina.weibo.object.Favorite;  
  24. import com.wwj.sina.weibo.object.Status;  
  25. import com.wwj.sina.weibo.util.LogUtils;  
  26.   
  27. /** 
  28.  * 微博数据请求监听器实现 
  29.  * @author wwj 
  30.  *  
  31.  */  
  32. public class StatusRequestListenerImpl implements RequestListener, Const {  
  33.   
  34.     private WeiboListAdapter weiboListAdapter;  
  35.     private WeiboListNoMoreAdapter weiboListNoMoreAdapter; // 隐藏“更多”  
  36.     private Activity activity;  
  37.     private View parent;  
  38.     private int type;  
  39.   
  40.     private Home homeActivity;  
  41.     private MessageViewer messageViewer;  
  42.   
  43.     private Handler handler = new Handler() {  
  44.         public void handleMessage(android.os.Message msg) {  
  45.             SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");  
  46.             String date = format.format(new Date());  
  47.             switch (type) {  
  48.             case HOME:  
  49.                 weiboListAdapter.putStatuses((List<Status>) msg.obj);  
  50.                 weiboListAdapter.hideMoreAnim();  
  51.                 weiboListAdapter.hideRefreshAnim();  
  52.                 homeActivity = (Home) activity;  
  53.                 if (homeActivity.weiboListView.getAdapter() == null) {  
  54.                     homeActivity.weiboListView.setAdapter(weiboListAdapter);  
  55.                     homeActivity.imageWorkQueueMonitor.addDoneAndProcess(HOME,  
  56.                             weiboListAdapter);  
  57.                 }  
  58.                 View homeReloadAnim = parent.findViewById(R.id.pb_home_reload);  
  59.                 View homeReload = parent.findViewById(R.id.btn_home_reload);  
  60.                 homeReloadAnim.setVisibility(View.GONE);  
  61.                 homeReload.setVisibility(View.VISIBLE);  
  62.   
  63.                 homeActivity.weiboListView.onRefreshComplete(date);  
  64.                 break;  
  65.             case MESSAGE_AT:  
  66.             case USER_TIMELINE:  
  67.                 weiboListAdapter.putStatuses((List<Status>) msg.obj);  
  68.                 weiboListAdapter.hideMoreAnim();  
  69.                 weiboListAdapter.hideRefreshAnim();  
  70.                 messageViewer = (MessageViewer) activity;  
  71.                 if (messageViewer.messageListView.getAdapter() == null) {  
  72.                     messageViewer.messageListView.setAdapter(weiboListAdapter);  
  73.                     messageViewer.imageWorkQueueMonitor.addDoneAndProcess(type,  
  74.                             weiboListAdapter);  
  75.                 }  
  76.                 View messageReloadAnim = parent  
  77.                         .findViewById(R.id.pb_message_reload);  
  78.                 View messageReload = parent  
  79.                         .findViewById(R.id.btn_message_reload);  
  80.                 messageReloadAnim.setVisibility(View.GONE);  
  81.                 messageReload.setVisibility(View.VISIBLE);  
  82.                 messageViewer.messageListView.onRefreshComplete(date);  
  83.                 break;  
  84.             case MESSAGE_FAVORITE:  
  85.                 weiboListNoMoreAdapter.putStatuses((List<Status>) msg.obj);  
  86.                 messageViewer = (MessageViewer) activity;  
  87.                 if (messageViewer.messageListView.getAdapter() == null) {  
  88.                     messageViewer.messageListView  
  89.                             .setAdapter(weiboListNoMoreAdapter);  
  90.                     messageViewer.imageWorkQueueMonitor.addDoneAndProcess(  
  91.                             MESSAGE_FAVORITE, weiboListNoMoreAdapter);  
  92.   
  93.                 }  
  94.                 View messageReloadAnim2 = parent  
  95.                         .findViewById(R.id.pb_message_reload);  
  96.                 View messageReload2 = parent  
  97.                         .findViewById(R.id.btn_message_reload);  
  98.                 messageReloadAnim2.setVisibility(View.GONE);  
  99.                 messageReload2.setVisibility(View.VISIBLE);  
  100.                 messageViewer.messageListView.onRefreshComplete(date);  
  101.   
  102.                 break;  
  103.             default:  
  104.                 break;  
  105.             }  
  106.             super.handleMessage(msg);  
  107.         };  
  108.     };  
  109.   
  110.     public StatusRequestListenerImpl(Activity activity, View parent, int type) {  
  111.         this.activity = activity;  
  112.         this.parent = parent;  
  113.         this.type = type;  
  114.     }  
  115.   
  116.     @Override  
  117.     public void onComplete(String response) {  
  118.         @SuppressWarnings("unchecked")  
  119.         List<Status> statuses = JSONAndObject.convert(Status.class, response,  
  120.                 "statuses");  
  121.         switch (type) {  
  122.         case HOME:  
  123.             LogUtils.d("### home");  
  124.             homeActivity = (Home) activity;  
  125.             if (homeActivity.homeTimelineAdapter != null) {  
  126.                 weiboListAdapter = homeActivity.homeTimelineAdapter;  
  127.             } else {  
  128.                 weiboListAdapter = new WeiboListAdapter(homeActivity, null,  
  129.                         type);  
  130.                 homeActivity.homeTimelineAdapter = weiboListAdapter;  
  131.             }  
  132.             break;  
  133.         case MESSAGE_AT:  
  134.         case USER_TIMELINE:  
  135.             LogUtils.d("### MESSAGE_AT");  
  136.             messageViewer = (MessageViewer) activity;  
  137.             if (messageViewer.messageListAdapter != null) {  
  138.                 weiboListAdapter = messageViewer.messageListAdapter;  
  139.             } else {  
  140.                 weiboListAdapter = new WeiboListAdapter(messageViewer, null,  
  141.                         type);  
  142.                 messageViewer.messageListAdapter = weiboListAdapter;  
  143.             }  
  144.             break;  
  145.         case MESSAGE_FAVORITE:  
  146.             LogUtils.d("### MESSAGE_FAVORITE");  
  147.             messageViewer = (MessageViewer) activity;  
  148.             if (messageViewer.weiboListNoMoreAdapter != null) {  
  149.                 weiboListNoMoreAdapter = messageViewer.weiboListNoMoreAdapter;  
  150.             } else {  
  151.                 weiboListNoMoreAdapter = new WeiboListNoMoreAdapter(  
  152.                         messageViewer, null, MESSAGE_FAVORITE);  
  153.                 messageViewer.messageListAdapter = weiboListNoMoreAdapter;  
  154.             }  
  155.   
  156.             List<Favorite> favorites = null;  
  157.             favorites = JSONAndObject.convert(Favorite.class, response,  
  158.                     "favorites");  
  159.             statuses = WeiboManager.FavoriteToStatus(favorites);  
  160.         default:  
  161.             break;  
  162.         }  
  163.         Message msg = new Message();  
  164.         msg.obj = statuses;  
  165.         handler.sendMessage(msg);  
  166.     }  
  167.   
  168.     @Override  
  169.     public void onIOException(IOException e) {  
  170.         e.printStackTrace();  
  171.     }  
  172.   
  173.     @Override  
  174.     public void onError(WeiboException e) {  
  175.         e.printStackTrace();  
  176.     }  
  177.   
  178. }  


这篇博客的关键点应该不在adapter这里,如何异步更新UI才是最重要的,我这里使用的是handler来更新UI。线程的异步操作就主要通过 AsyncWeiboRunner这个类实现,这部分内容就已经囊括了其他微博数据的获取实现流程,获取收藏列表和提到我的微博列表都是类似的实现,我就不多介绍了
http://www.lbrq.cn/news/1359397.html

相关文章:

  • 制作一个网站怎么做的/武汉seo排名公司
  • 莞城网站仿做/上首页的seo关键词优化
  • 客户说做网站价格高/网游推广
  • vue做前台网站/简述seo
  • 专业的深圳网站建设公司/安卓优化神器
  • 哪个网站能在家做兼职/免费快速网站
  • 承德网站制作加盟/免费找客户软件
  • 找别人做的网站问什么域名解析后还是上线不/最新发布的最新
  • 达州网站制作/seo 优化教程
  • eclipse sdk做网站/微信做单30元一单
  • 上海 网站工作室/广州seo招聘网
  • 如何推广个人网站/广州网站优化软件
  • 建设168网站/疫情防控最新通告
  • 微商城新零售app/上海优化网站seo公司
  • 车票在线制作网站/怎么做百度推广运营
  • 政府网站建设的基本流程/关键词优化需要从哪些方面开展
  • 禅城区做网站策划/今天的三个新闻
  • 济南网站制作套餐/大连seo网站推广
  • 最专业的礼品网站案例参考/免费创建个人网页
  • 做政府门户网站建设/江苏网站seo设计
  • 北京网站建设有哪些/关键词排名优化易下拉技术
  • 做企业网站需要人维护么/前端seo主要优化哪些
  • 长春市做网站/域名权重
  • 网站建设自评报告/网络推广网站推广
  • 小白自己做网站/域名邮箱 400电话
  • 网站设置支付宝在线支付/官网百度
  • 企业官方网站地址/seo推广百度百科
  • 香港做指数的网站/贵州seo和网络推广
  • 品牌网站建设k小蝌蚪/商品推广软文写作500字
  • 新乡做网站公司电话/百度竞价排名机制
  • Agent安全机制:权限控制与风险防范
  • Shader开发(七)创建第一个Shader项目
  • 【基础】第八篇 Java 位运算符详解:从基础到实战应用
  • SELinux加固Linux安全
  • 力扣1124:表现良好的最长时间段
  • 《Python 实用项目与工具制作指南》· 2.3 导入