赞
踩
上篇已完成了微博的发送,接下来我们就以“主页时间线来获取微博”
1.首先,根据返回的json微博数据,我们需要对json数据进行解析
返回的json数据格式如下:
{"data":
{
"hasnext":0,
"info":[
{
"city_code":"2",
"count":0,
"country_code":"1",
"emotiontype":0,
"emotionurl":"",
"from":"微博开放平台",
"fromurl":"http:\/\/wiki.open.t.qq.com\/index.php\/产品类FAQ#.E6.8F.90.E4.BA.A4.E5.BA.94.E7.94.A8.E6.9D.A5.E6.BA.90.E5.AD.97.E6.AE.B5.E5.AE.A1.E6.A0.B8.E8.83.BD.E5.BE.97.E5.88.B0.E4.BB.80.E4.B9.88.E5.A5.BD.E5.A4.84.EF.BC.9F\u000a","geo":"","head":"","id":"179515026443503",
"image":null,
"isrealname":2,
"isvip":0,
"jing":"0",
"latitude":"0",
"location":"中国 湖南 株洲",
"longitude":"0",
"mcount":0,
"music":null,
"name":"xiexieni4185",
"nick":"谢谢你",
"openid":"7CE51577F1CD290A03784DB817622898",
"origtext":"yyyyyy",
"province_code":"43",
"self":1,
"source":null
,"status":0,
"text":"yyyyyy",
"timestamp":1355600954,
"type":1,
"video":null,
"wei":"0"
}
],
"timestamp":1355600965,
"user":{
"xiexieni4185":"谢谢你"
}
},
"errcode":0,
"msg":"ok",
"ret":0,
"seqid":5822261811111418032
}
-------------------------分析 :
{"data":
{
"hasnext":0,
"info":[
{},
{}
],
"timestamp":1355600965,
"user":{
"xiexieni4185":"谢谢你"
}
},
"errcode":0,
"msg":"ok",
"ret":0,
"seqid":5822261811111418032
}
--------------------------------------------------------
json数据解析的工具类代码:
package com.xgh.tencent.weibo.data.util;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.xgh.tencent.weibo.beans.Blog;
import com.xgh.tencent.weibo.beans.Data;
import com.xgh.tencent.weibo.beans.DataContext;
import com.xgh.tencent.weibo.beans.Info;
import com.xgh.tencent.weibo.beans.User;
public class JSONParser {
public static Data getData(String jsonString)
{
List blogList = new
ArrayList();
// 获取最上层json数据对象
JSONObject dataContextJO =
parseJSONString(jsonString);
// 创建数组上下文对象
DataContext dataContext = new
DataContext();
Data data = null;
try {
dataContext.errcode
= dataContextJO.getString("errcode");
dataContext.msg
= dataContextJO.getString("msg");
dataContext.ret
= dataContextJO.getString("ret");
dataContext.seqid
= dataContextJO.getString("seqid");
//
创建Data对象
data = new Data();
JSONObject
dataJO = dataContextJO.getJSONObject("data");
data.context
= dataContext;
data.hasnext
= dataJO.getString("hasnext");
data.timestamp
= dataJO.getString("timestamp");
// 获取用户
JSONObject
userJO = dataJO.getJSONObject("user");
//
创建User对象
User user =
new User();
//
..暂时不解析
//
解析Info
JSONArray
infoJA = dataJO.getJSONArray("info");
//
创建Info对象
Info info =
new Info();
//
解析BlogList
JSONObject
jsonObj = null;
for (int i =
0; i < infoJA.length(); i++) {
Blog
blog = new Blog();
jsonObj
= infoJA.getJSONObject(i);
blog.id
= jsonObj.getString("id");
blog.text
= jsonObj.getString("text");
blog.origtext
= jsonObj.getString("origtext");
blog.openid
= jsonObj.getString("openid");
blog.nick
= jsonObj.getString("nick");
blog.name
= jsonObj.getString("name");
blog.timestamp
= jsonObj.getString("timestamp");
blog.count
= jsonObj.getString("count");
blog.from
= jsonObj.getString("from");
blog.fromurl
= jsonObj.getString("fromurl");
blog.self
= jsonObj.getString("self");
blog.status
= jsonObj.getString("status");
blog.location
= jsonObj.getString("location");
blog.latitude
= jsonObj.getString("latitude");
blog.longitude
= jsonObj.getString("longitude");
//
添加到博客列表
blogList.add(blog);
}
info.blogs =
blogList;
//
设置数据关系
data.context
= dataContext;
data.user =
user;
data.info =
info;
} catch (JSONException e)
{
e.printStackTrace();
}
return data;
}
public static List getBlogList(Data data) {
return data.info.blogs;
}
public static JSONObject parseJSONString(String
jsonString) {
JSONObject jsonObj =
null;
try {
jsonObj = new
JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObj;
}
}
----------------------------------------------------------
根据返回的微博JSON数据,自定义的一些模型类:
微博model类:
package com.xgh.tencent.weibo.beans;
import java.util.Date;
public class Blog {
public String name; //似为昵称拼音
public String city_code; //城市码
public String count; //微博被转发的次数
public String text; //微博内容
public String origtext; //原始内容
public String mcount; //点评次数
public String
from; //微博来源
public String fromurl; //来源url
public String
id; //微博唯一id
public String image; //图片url列表
public String status;
//微博状态,0-正常,1-系统删除,2-审核中,3-用户删除,4-根删除(根节点被系统审核删除)
public String nick; //用户昵称
public String openid; //用户openid
public String provicen_code; //省代码
public String self; //是否自已发的的微博,0-不是,1-是
public
String head; //发表者头像url;
public
String timestamp; //发表时间
public
String latitude ; //经度
public
String longitude; //纬度
public
String location; //位置
public Video video; //视频信息
public Music music; //音频信息
public String toString(){
return
"id:"+id+",openid:"+openid+",name:"+name+",nick:"+nick
+",text:"+text+",origtext:"+origtext+",self:"+self+
",status:"+status+",timestamp:"+timestamp+",from:"+from
+",location:"+location+",count:"+count+",fromurl:"+fromurl;
}
public String getGeneralShowMsg(){
return origtext+","+nick+","+
timestamp;
}
}
------------------------------------------------
package com.xgh.tencent.weibo.beans;
public class Data {
public String timestamp; //时间线
public String hasnext; //是否还有,0表示有
public User user; //用户信息
public Info info; //信息,包含博客内容列表
public DataContext context; //数组上下文信息
public String toString(){
return
"hasnext:"+hasnext+",timestamp:"+timestamp
+",user{"+user.toString()+"},info{"+info.toString()
+"},context{"+context.toString()+"}";
}
}
------------------------------------------------------------
package com.xgh.tencent.weibo.beans;
public class DataContext { public
String errcode; //错误码 , 0表示正常返回
public
String
msg; //返回信息,正常时返回"ok"
public
String
ret; //正常时,返回0
public
String seqid; //序列id
public
String toString(){
return
"errcode:"+errcode+",msg:"+msg
+",ret:"+ret+",seqid:"+seqid
;
}
}
------------------------------------------------
package com.xgh.tencent.weibo.beans;
import java.util.List;
public class Info {
public List
blogs; //微博数组
public
String toString(){
if(blogs==null||blogs.size()==0)
return "{null}";
StringBuffer buff = new StringBuffer("{");
for(Blog blog: blogs){
buff.append(blog.toString()).append(",");
}
buff.append("}");
return buff.toString();
}
}
--------------------------------------------
package com.xgh.tencent.weibo.beans;
public class User {
public String name; //用户名
public String nick; //昵称
public String toString(){
return
"name:"+name+",nick:"+nick;
}
}
----------------------------------------
package com.xgh.tencent.weibo.beans;
public class Music {
}
-------------------------------------------
package com.xgh.tencent.weibo.beans;
public class Image {
}
---------------------------------------------
package com.xgh.tencent.weibo.beans;
public class Video {
}
另外,还有一个临时的BeanUtil类:
package com.xgh.tencent.weibo.data.util;
import java.util.ArrayList;
import java.util.List;
import com.xgh.tencent.weibo.beans.Blog;
public class BeanUtil {
public static
List
objectToStringList(List extends
Object> list){
List result = new
ArrayList();
for(Object obj:list){
result.add(obj.toString());
}
return result;
}
public static
List
getBlogStringList(List
blogs){
List result = new
ArrayList();
for(Blog obj:blogs){
result.add(obj.getGeneralShowMsg());
}
return result;
}
}
======================================================================
2.
以主页时间线,获取微博列表
可参考此链接:
代码:(红色部分)
package com.xgh.tecent.weibo.view;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.tencent.weibo.api.RequestAPI;
import com.tencent.weibo.api.Statuses_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.beans.QParameter;
import com.xgh.tencent.weibo.beans.Data;
import com.xgh.tencent.weibo.context.ApplicationConstants;
import com.xgh.tencent.weibo.context.QQContext;
import com.xgh.tencent.weibo.data.dao.DataHelper;
import com.xgh.tencent.weibo.data.util.BeanUtil;
import com.xgh.tencent.weibo.data.util.JSONParser;
public class MainPaneActivity extends Activity {
private TextView tvShow1;
// 微博内容输入框
private EditText etInputBlog;
// 发送微博的按钮
private Button butSendBlog;
// 微博列表显示控件
private ListView blogList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pane);
// 界面控件获取
etInputBlog = (EditText)
findViewById(R.id.inputBlog);
tvShow1 = (TextView)
findViewById(R.id.tvShow1);
blogList = (ListView)
findViewById(R.id.lvBlogList);
// 添加事件监听
butSendBlog = (Button)
findViewById(R.id.butSend);
butSendBlog.setOnClickListener(new
OnClickListener() {
public void
onClick(View v) {
//
获取微博内容
String
blog = etInputBlog.getText().toString();
//
如果内容为空,则是不发送
if
(blog == null || "".equals(blog.trim())) {
Toast.makeText(MainPaneActivity.this,
"内容不能为空",
Toast.LENGTH_SHORT).show();
return;
}
//
发送微博
RequestAPI
rAPI = new RequestAPI();
List
otherParamas = new
ArrayList();
otherParamas.add(new
QParameter("content", blog));
otherParamas.add(new
QParameter("format ", "json"));
try
{
//
发送一条微博信息
rAPI.postContent(ApplicationConstants.URL_POST_BLOG,
otherParamas,
QQContext.oauth);
//
提示
Toast.makeText(QQContext.mainActivity,
"已发送。。",
Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
// 打印token
findViewById(R.id.butGetToken).setOnClickListener(
new
OnClickListener() {
public
void onClick(View v) {
Log.i("xgh:",
"-----------------------------------------");
Log.i("xgh:finalToken:",
""
+ QQContext.oauth.getOauth_token());
Log.i("xgh:finalSecret:",
""
+ QQContext.oauth.getOauth_token_secret());
Log.i("xgh:finalVerifier:",
""
+ QQContext.oauth.getOauth_verifier());
}
});
//
获取微博列表
findViewById(R.id.butGetBlogList).setOnClickListener(
new
OnClickListener() {
public
void onClick(View v) {
try
{
Statuses_API
sa = new Statuses_API();
System.out.println("-----------------");
String
blogDataString = sa.home_timeline(
QQContext.oauth,
"json",
"0", //分页标识(0:第一页,1:向下翻页,2:向上翻页
"0", //本页起始时间(第一页:填0,向上翻页:填上一次请求返回的第一条记录时间,向下翻页:填上一次请求返回的最后一条记录时间)
"4" //每次请求记录的条数(1-70条)
);
Data
data = JSONParser.getData(blogDataString);
//
转换出数据
List
datas = BeanUtil
.getBlogStringList(data.info.blogs);
//
将数据加入到界面
ArrayAdapter
adapter = new
ArrayAdapter(
MainPaneActivity.this,
android.R.layout.simple_list_item_1,
datas);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。