新浪微博(二十三)操作一条微博(SingleWeiBoActivity 类)

来源:互联网 发布:山东人才管理网络系统 编辑:程序博客网 时间:2024/05/17 07:13
效果图:
昨天完成了对微博数据的获取过程,今天要完成对点击每一条微博信息的响应,单独弹出一个dialog进行一条微博的评论、收藏、转发功能。另外,完成发微薄的功能。
在这个过程中,要弄明白的知识点有:json解析的实现细节;评论、收藏、转发、发表微博的参数和函数;如何对交互页面进行布局。
关于JSON解析其实很简单,既然要解析数据,就要知道数据是怎样被封装起来的,官方API给出了详细的说明和示例:(你应该参照的是1.0的API文档,而不是默认显示的2.0API因此,解析的时候,要先使用JSONArray,然后是JSONObject,第一层的JSONObject里面出来单个的子元素之外,还有JSONObject数组(如上图中的user)。知道了这一点就可以使用get***方法获取数据了。如果对JSONObject不熟悉的话,可以参照http://blog.csdn.net/logan676/article/details/7542720。
既然要实现发表微博的功能,就要对窗口左上方的发表微博按钮(实际上是一个imageView)进行事件的监听和相应。
既然微博是动态加载的,也就是要定时刷新或者点击刷新按钮,才能获取最新的微博动态,因而也要对刷新按钮进行事件的监听。
package tianyi.sina.weibo;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import tianyi.oauth.OauthUtils;import tianyi.sina.R;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.Html;import android.text.method.LinkMovementMethod;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class SingleWeiBoActivity extends Activity {// 头像private ImageView imageView;// 收藏private Button button_ShouCang;// 转发private Button button_ZhuanFa;// 评论private Button button_PingLun;// 返回private Button button_Return;// 微博的标题TextView name = null;// 微博的内容TextView content = null;// 微博的评论// TextView comments = null;// 微博来自哪个平台TextView whichPlatform = null;// 递交数据的方式String httpMethod = "GET";// 微博的ID号String weiBoID = null;// 二级微博的图片ImageView attachedPhotos = null;// 二级微博的文字内容TextView attachedText = null;String num = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.weibo_singleweibo);Intent intent = getIntent();// 用来标识这条微博的ID号weiBoID = intent.getStringExtra("weiboID");// 一个页面要显示的微博条数num = intent.getStringExtra("num");// 收藏button_ShouCang = (Button) findViewById(R.id.weibo_singleweibo_button_shouCang);button_ShouCang.setOnClickListener(new Button_ShouCangListener());// 收藏button_ZhuanFa = (Button) findViewById(R.id.weibo_singleweibo_button_zhuanFa);button_ZhuanFa.setOnClickListener(new Button_ZhuanFaListener());// 评论button_PingLun = (Button) findViewById(R.id.weibo_singleweibo_button_pingLun);button_PingLun.setOnClickListener(new Button_PingLunListener());// 返回button_Return = (Button) findViewById(R.id.weibo_singleweibo_button_return);button_Return.setOnClickListener(new Button_ReturnListener());imageView = (ImageView) findViewById(R.id.weibo_singleweibo_imageView);displaySingleWeiBoContent();}/* * 这是一个现实单条微博内容的函数 他除了可以显示微博的文字内容之外,还负责显示微博的标题、微博的头像、转发信息、收藏信息等等 * 很显然,欲达到上述功能,它需要调用其他的函数和类的应用来实现 * 因此,研究这个函数里引用的类和函数才能更好的理解这个函数,才能更好地理解操作微博的具体流程和细节 */private void displaySingleWeiBoContent() {// 微博的标题name = (TextView) findViewById(R.id.weibo_singleweibo_textView_name);// 微博的文字内容content = (TextView) findViewById(R.id.weibo_singleweibo_textView_content);// 评论// comments = (TextView)// findViewById(R.id.weibo_singleweibo_textView_textView_textView1_comments);// 微博来自哪个平台whichPlatform = (TextView) findViewById(R.id.weibo_singleweibo_textView_textView_whichPlatform);// 微博头像imageView = (ImageView) findViewById(R.id.weibo_singleweibo_imageView);attachedPhotos = (ImageView) findViewById(R.id.weibo_singleweibo_textView_textView_imageView1_attachedPhotos);// 二级微博attachedText = (TextView) findViewById(R.id.weibo_singleweibo_textView_textView_textView1_attachedText);String singleWeiBoInfo = null;// 配置文件SharedPreferences pres = this.getSharedPreferences("parameters",Context.MODE_PRIVATE);// 由于access_token和access_token_secret都是一次申请到,永久使用的,因而当用户一次认证后,无需再次验证。// 因而直接把它们保存在配置文件中,以备下次使用String sina_access_token = pres.getString("sina_access_token", "");String sina_access_secret = pres.getString("sina_access_secret", "");// 这是旧版的url地址,由于我使用的是)Oauth1.0进行开发的,因此使用2.0的url无法获取数据,下面是最新的urlString url = "http://api.t.sina.com.cn/statuses/friends_timeline.json";/* * 获取当前登录用户及其所关注用户的最新微博 URL * https://api.weibo.com/2/statuses/friends_timeline.json * 要注意的是:你可能会想到使用“根据微博的ID获取微博信息的那个官方URL”。 * 但是疑惑的是,经过测试发现程序仍旧报空指针异常的错误,至于为什么我也不太清楚 */OauthUtils.getInstance().initSinaData();/* *  * 根据微博ID获取单条微博内容 URL:https://api.weibo.com/2/statuses/show.json * 支持格式:JSON HTTP请求方式:GET 是否需要登录:是 */singleWeiBoInfo = new Weibo().getWeiBoList(url, httpMethod,sina_access_token, sina_access_secret, null, null, null, null,num);JSONArray info = null;try {info = new JSONArray(singleWeiBoInfo);} catch (JSONException e) {e.printStackTrace();}// 传入单条微博的id号JSONObject test = info.optJSONObject(Integer.parseInt(weiBoID));JSONObject user = test.optJSONObject("user");JSONObject retweet = test.optJSONObject("retweeted_status");String whichPlatformStr = test.optString("source");whichPlatform.setText(Html.fromHtml(whichPlatformStr));whichPlatform.setTextColor(Color.WHITE);// 链接whichPlatform.setMovementMethod(LinkMovementMethod.getInstance());String text = test.optString("text");// 显示微博内容content.setText(text);String screen_name = user.optString("name");name.setText(screen_name);String imgurl = user.optString("profile_image_url");imageView.setImageDrawable(loadImage(imgurl));if (retweet != null) {JSONObject retuser = retweet.optJSONObject("user");String rettext = retweet.optString("text");String retimg = retweet.optString("thumbnail_pic");if (retimg != null) {attachedPhotos.setImageDrawable(loadImage(retimg));}String retscreen_name = retuser.optString("name");attachedText.setText(retscreen_name + ":" + rettext);}}/** * 下载头像 *  * @param url * @return */private Drawable loadImage(String url) {try {return Drawable.createFromStream((InputStream) new URL(url).getContent(), "imgname");// 图片地址} catch (MalformedURLException e) {Log.e("exception", e.getMessage());} catch (IOException e) {Log.e("exception", e.getMessage());}return null;}class Button_ShouCangListener implements OnClickListener {// 收藏按钮@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}class Button_ZhuanFaListener implements OnClickListener {// 转发按钮@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}class Button_PingLunListener implements OnClickListener {// 评论按钮@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}class Button_ReturnListener implements OnClickListener {// 返回按钮@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfinish();}}}
weibo_singleweibo.xml文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/weibo_singleweibo_imageView"        android:layout_width="50dp"        android:layout_height="50dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" />    <Button        android:id="@+id/weibo_singleweibo_button_pingLun"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@+id/weibo_singleweibo_imageView"        android:layout_marginRight="93dp"        android:layout_marginTop="204dp"        android:text="评论" />    <Button        android:id="@+id/weibo_singleweibo_button_shouCang"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/weibo_singleweibo_button_pingLun"        android:layout_alignBottom="@+id/weibo_singleweibo_button_pingLun"        android:layout_marginRight="19dp"        android:layout_toLeftOf="@+id/weibo_singleweibo_button_pingLun"        android:text="收藏" />    <Button        android:id="@+id/weibo_singleweibo_button_return"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/weibo_singleweibo_button_pingLun"        android:layout_alignBottom="@+id/weibo_singleweibo_button_pingLun"        android:layout_alignParentRight="true"        android:layout_marginRight="25dp"        android:text="返回" />    <Button        android:id="@+id/weibo_singleweibo_button_zhuanFa"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/weibo_singleweibo_button_shouCang"        android:layout_alignBottom="@+id/weibo_singleweibo_button_shouCang"        android:layout_marginRight="24dp"        android:layout_toLeftOf="@+id/weibo_singleweibo_button_shouCang"        android:text="转发" />    <TextView        android:id="@+id/weibo_singleweibo_textView_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/weibo_singleweibo_imageView"        android:layout_alignLeft="@+id/weibo_singleweibo_button_shouCang"        android:textColor="#ffffff"        android:textSize="20dp" />    <TextView        android:id="@+id/weibo_singleweibo_textView_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/weibo_singleweibo_textView_name"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:layout_marginTop="20dp"        android:textColor="#ffffff"        android:textSize="14dp" />    <TextView        android:id="@+id/weibo_singleweibo_textView_textView_textView1_attachedText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/weibo_singleweibo_textView_content"        android:layout_alignParentRight="true"        android:layout_below="@+id/weibo_singleweibo_textView_content" />    <ImageView        android:id="@+id/weibo_singleweibo_textView_textView_imageView1_attachedPhotos"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_above="@+id/weibo_singleweibo_textView_textView_whichPlatform"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_below="@+id/weibo_singleweibo_textView_textView_textView1_attachedText" />    <TextView        android:id="@+id/weibo_singleweibo_textView_textView_whichPlatform"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/weibo_singleweibo_button_zhuanFa"        android:layout_alignLeft="@+id/weibo_singleweibo_textView_textView_textView1_attachedText"        android:textColor="#ffffff" /></RelativeLayout>
另外一点值得探讨的是,当文字信息过多,TextView无法完全显示的时候,如果不进行设置的话,会挤占其他控件的空间。遗憾的是,目前我还没有找到解决它的办法,,,,

对于微博ID号:
    1.contentActivity——传值——ListView中选中的那条微博的position值——SingleActivity——获取该微博
    2.在SingleActivity类中,根据position的值解析在JSONArray中对应的JSONObject,获取微博数据。并获取微博的ID号
    3.在对发表、收藏、评论微博的事件处理中使用Intent将ID号传给下一个激活的activity。新的activity使用ID号,完成对微博的发表、收藏、评论操作。
另外一点要注意的是:由于微博列表时刻在更新着,也就意味着用户当前的微博列表很可能不是最新的,的那样的话,使用position获取到的一条微博可能滞后了的哪一条,导致数据不吻合。对于解决方案,研究中~

原创粉丝点击