在写上一篇文章时,发现一个东西忘记讲了,就是如何获取数据?在我的微博里,我设计了一个公共类,这个类主要是通过参数调用新浪接口获取json数据。然后我们通过json解释出我们需要的对象集合。
这个类主要net包里Oauth.java
这个类没什么好讲的,主要是json解释。看下一般就明白,以后 我们获取数据就直接调用这个类就行了。
Oauth.java
package com.otcyan.weibo.net;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import otcyan.otcyan.weibo.servieces.Collect;
import otcyan.otcyan.weibo.servieces.CommentInfo;
import otcyan.otcyan.weibo.servieces.Fans;
import otcyan.otcyan.weibo.servieces.MyInfo;
import otcyan.otcyan.weibo.servieces.RepeatWeiboInfo;
import otcyan.otcyan.weibo.servieces.UserInfo;
import otcyan.otcyan.weibo.servieces.WeiBoInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;
import com.otcyan.weibo.tools.Tookit;
import com.weibo.net.AccessToken;
import com.weibo.net.Oauth2AccessTokenHeader;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;
/**
*
* @author Administrator otcyan
*
*/
public class Oauth {
private Context context = null;
public Oauth(Context context ){
this.context = context ;
}
/**
* 登录授权类
* @return 返回一个true已经授权 false表示 还没有授权
*/
public boolean login(){
SharedPreferences preferences = context.getSharedPreferences(OtWeibo.SHARED_OAUTH, 0) ;
//Log.v("otcyan", preferences.getString(OtWeibo.SHARED_OAUTH, null)) ;
//判断是已经验证过
if(preferences.getString(OtWeibo.ACCESS_TOKEN,null)==null){
//还没有授权 进入 一个activity进行授权
return false ;
}else {
//已经进行过授权管理
Weibo weibo = Weibo.getInstance() ;
weibo.setupConsumerConfig(OtWeibo.CONSUMER_KEY, OtWeibo.CONSUMER_SECRET) ;
String token = preferences.getString(OtWeibo.ACCESS_TOKEN, null) ;
String expire_in = preferences.getString(OtWeibo.EXPIRES_IN, null) ;
AccessToken accessToken = new AccessToken(token, OtWeibo.CONSUMER_SECRET) ;
accessToken.setExpiresIn(expire_in) ;
//设置 weibo 的accessToken
weibo.setAccessToken(accessToken) ;
return true ;
}
}
public String queryInfoByUrl(String url , WeiboParameters params) throws WeiboException{
Weibo weibo = Weibo.getInstance() ;
return weibo.request(context, url, params, Utility.HTTPMETHOD_POST, weibo.getAccessToken()) ;
}
/**
* 得到所有好友信息 信息
* @return
* @throws WeiboException
*/
public String getFriendsTimeLineToString(Weibo weibo , int page) throws WeiboException{
String url = Weibo.SERVER+"statuses/friends_timeline.json" ;
Log.v("otcyan", url) ;
//这是微博参数
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
params.add("page", page+"") ; //显示 的页数
params.add("count", page*10+"") ; //显示的数据总数
Log.v("otcyan", weibo.getAccessToken().getToken()) ;
String info = weibo.request(context, url, params, "GET", weibo.getAccessToken()) ;
return info ;
}
/**
* 得到uid的所有 微博信息
* @return
* @throws WeiboException
*/
public String getAllInfoBYId(Weibo weibo , int page , String id ) throws WeiboException{
String url = Weibo.SERVER+"statuses/user_timeline.json" ;
Log.v("otcyan", url) ;
//这是微博参数
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
//params.add("page", page+"") ; //显示 的页数
params.add("count", page*20+"") ; //显示的数据总数
params.add("uid", id) ;
Log.v("otcyan", weibo.getAccessToken().getToken()) ;
String info = weibo.request(context, url, params, "GET", weibo.getAccessToken()) ;
return info ;
}
/**
* 得到 登录自己的信息
* @param weibo weibo
* @param uid uid
* @return 我的登录信息
* @throws WeiboException
* @throws JSONException
*/
public MyInfo getMyInfo(Weibo weibo , String uid) throws WeiboException, JSONException{
String url = Weibo.SERVER+"users/show.json";
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
params.add(OtWeibo.UID, uid) ;
String str = weibo.request(context, url, params, "GET", weibo.getAccessToken()) ;
Log.v("otcyan", str) ;
//对info进行json解析
JSONObject obj = new JSONObject(str) ;
MyInfo myInfo = MyInfo.getInstance() ;
myInfo.uid = obj.getString("id") ;
myInfo.name = obj.getString("screen_name") ;
Log.v("otcyan", myInfo.name) ;
myInfo.statuses_count = obj.getInt("statuses_count") ;
myInfo.imageUrl = obj.getString("profile_image_url") ;
myInfo.gender = obj.getString("gender") ;
myInfo.bigUrl = obj.optString("avatar_large") ;
myInfo.description = obj.optString("description") ;
myInfo.favourites_count = obj.getInt("favourites_count") ;
myInfo.friends_count = obj.getInt("friends_count") ;
myInfo.followers_count = obj.getInt("followers_count") ;
myInfo.created_at = obj.getString("created_at") ;
myInfo.location = obj.getString("location") ;
return myInfo ;
}
/**
*
* @param info 得到 的json数据
* @return 返回weiboInfo集合
* @throws JSONException
*/
public List<WeiBoInfo> getFriendsTimeLineToList(String info) throws JSONException{
List<WeiBoInfo> weiboList = new ArrayList<WeiBoInfo>() ;
JSONObject obj = new JSONObject(info) ;
JSONArray data = obj.getJSONArray("statuses") ;
for (int i = 0; i < data.length(); i++) {
//Log.v("otcyan", "data.id"+data.get("id")) ;
//得到一个json对象
JSONObject d = data.getJSONObject(i) ;
// Log.v("otcyan", d.getString("created_at")) ;
//创建 一个 WeiboInfo
WeiBoInfo weiBoInfo = new WeiBoInfo() ;
if(d!=null){
//Log.v("otcyan", "d不为空") ;
weiBoInfo.favorited = d.getBoolean("favorited") ;
weiBoInfo.id = d.getString("id") ;
weiBoInfo.time = Tookit.formatShowTime(d.getString("created_at")) ;
weiBoInfo.content = d.getString("text") ;
weiBoInfo.repost = d.getInt("reposts_count") ;
weiBoInfo.comment = d.getInt("comments_count") ;
weiBoInfo.source = d.optString("source") ;
weiBoInfo.imageUrl = d.optString("thumbnail_pic") ; //中等的缩略图
Log.v("otcyan", d.has("retweeted_status")+"") ;
//转发微博信息
if(d.has("retweeted_status")){
Log.v("otcyan", "--------->retweeted_status") ;
JSONObject r=d.getJSONObject("retweeted_status");
RepeatWeiboInfo reWeibo = new RepeatWeiboInfo() ;
reWeibo.content = r.optString("text") ; //转发微博文字内容
reWeibo.imageUrl = r.optString("thumbnail_pic") ; //转发微博图片内容
weiBoInfo.repeatWeiboInfo = reWeibo ;
Log.v("otcyan", "测试转发:"+weiBoInfo.repeatWeiboInfo.content+"----------->imageUrl"+weiBoInfo.repeatWeiboInfo.imageUrl) ;
}
//表示有数据
JSONObject u = d.getJSONObject("user") ;
if(u!=null){
//创建一个用户信息
UserInfo userInfo = new UserInfo() ;
userInfo.uid = u.getString("id") ;
userInfo.name = u.getString("screen_name") ;
userInfo.imageUrl = u.getString("profile_image_url") ;
userInfo.bigUrl = u.optString("avatar_large") ;
userInfo.description = u.optString("description") ;
userInfo.favourites_count = u.getInt("favourites_count") ;
userInfo.followers_count = u.getInt("followers_count") ;
userInfo.friends_count = u.getInt("friends_count") ;
userInfo.gender = u.getString("gender") ;
userInfo.location = u.getString("location") ;
userInfo.statuses_count = u.getInt("statuses_count") ;
//Log.v("otcyan", "userInfo is \n"+userInfo) ;
weiBoInfo.userInfo = userInfo ;
//Log.v("otcyan", "weiBoInfo is \n"+weiBoInfo) ;
}
// JSONObject r = d.getJSONObject("retweeted_status") ;
// if(r!=null){
// //有转发的微博
// weiBoInfo.repeatWeiboInfo.content = r.getString("text") ;
// }
weiboList.add(weiBoInfo) ;
}
}
return weiboList ;
}
/**
* 得到 微博的信息
* @param weibo 一个微博实例
* @param id 微博的id
* @return 微博的信息
* @throws WeiboException
* @throws JSONException
*/
public WeiBoInfo getUserWeiBoInfo(Weibo weibo , String id) throws WeiboException, JSONException{
String url = Weibo.SERVER+"statuses/show.json";
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
params.add("id", id) ;
String str = weibo.request(context, url, params, "GET", weibo.getAccessToken()) ;
WeiBoInfo weiBoInfo = new WeiBoInfo() ;
//进行json解析
JSONObject d = new JSONObject(str) ;
if(d!=null){
//Log.v("otcyan", "d不为空") ;
weiBoInfo.id = d.getString("id") ;
weiBoInfo.time = Tookit.formatShowTime(d.getString("created_at")) ;
weiBoInfo.content = d.getString("text") ;
weiBoInfo.repost = d.getInt("reposts_count") ;
weiBoInfo.comment = d.getInt("comments_count") ;
weiBoInfo.imageUrl = d.optString("original_pic") ; //微博的缩略图
Log.v("otcyan", d.has("retweeted_status")+"") ;
//转发微博信息
if(d.has("retweeted_status")){
Log.v("otcyan", "--------->retweeted_status") ;
JSONObject r=d.getJSONObject("retweeted_status");
RepeatWeiboInfo reWeibo = new RepeatWeiboInfo() ;
reWeibo.content = r.optString("text") ; //转发微博文字内容
reWeibo.imageUrl = r.optString("thumbnail_pic") ; //转发微博图片内容
weiBoInfo.repeatWeiboInfo = reWeibo ;
Log.v("otcyan", "测试转发:"+weiBoInfo.repeatWeiboInfo.content+"----------->imageUrl"+weiBoInfo.repeatWeiboInfo.imageUrl) ;
}
//表示有数据
JSONObject u = d.getJSONObject("user") ;
if(u!=null){
//创建一个用户信息
UserInfo userInfo = new UserInfo() ;
userInfo.uid = u.getString("id") ;
userInfo.name = u.getString("screen_name") ;
userInfo.imageUrl = u.getString("profile_image_url") ;
//Log.v("otcyan", "userInfo is \n"+userInfo) ;
weiBoInfo.userInfo = userInfo ;
//Log.v("otcyan", "weiBoInfo is \n"+weiBoInfo) ;
}
}
return weiBoInfo ;
}
/**
*
* @param weibo
* @param id
* @return
* @throws WeiboException
* @throws JSONException
*/
public List<Fans> getFans(Weibo weibo , String id , int page) throws WeiboException, JSONException{
String url = Weibo.SERVER+"friendships/followers.json" ;
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
params.add("uid", id) ;
params.add("count", page*20+"") ;
params.add("trim_status", 0+"") ;
String info = weibo.request(context, url, params, Utility.HTTPMETHOD_GET, weibo.getAccessToken()) ;
Log.v("home", "fans:"+info) ;
List<Fans> list = new ArrayList<Fans>() ;
JSONObject obj = new JSONObject(info) ;
if(obj.has("users")){
JSONArray data = obj.getJSONArray("users") ;
for (int i = 0; i < data.length(); i++) {
JSONObject u = data.getJSONObject(i) ;
Fans fans = new Fans() ;
fans.bigImageUrl = u.optString("avatar_large") ;
fans.description = u.optString("description") ;
fans.favourites_count = u.optInt("favourites_count") ;
fans.followers_count = u.optInt("followers_count") ;
fans.friends_count = u.optInt("friends_count") ;
fans.gender = u.optString("gender") ;
fans.id = u.getString("id") ;
fans.location = u.optString("location") ;
fans.name = u.optString("name") ;
fans.smallImageUrl = u.optString("profile_image_url") ;
fans.statuses_count = u.optInt("statuses_count") ;
fans.weihao = u.optString("weihao") ;
list.add(fans) ;
}
}
return list ;
}
/**
* 得到 我收到 的微博列表
* @param weibo
* @param params
* @return
* @throws WeiboException
* @throws JSONException
*/
public List<CommentInfo> getCommentInfo(Weibo weibo , WeiboParameters params , String url) throws WeiboException, JSONException{
String info = weibo.request(context, url, params, Utility.HTTPMETHOD_GET, weibo.getAccessToken()) ;
Log.v("otcyan", info) ;
//json解释
JSONObject o = new JSONObject(info) ;
JSONArray data = o.getJSONArray("comments") ;
List<CommentInfo> comments = new ArrayList<CommentInfo>() ;
for (int i = 0; i < data.length(); i++) {
CommentInfo comment = new CommentInfo() ;
JSONObject obj = data.getJSONObject(i) ;
comment.created_at = Tookit.formatShowTime(obj.optString("created_at")) ; //评论的创建时间
comment.id = obj.getString("id") ;
comment.source = obj.optString("source") ;
comment.text = obj.optString("text") ;
JSONObject commentUser = obj.getJSONObject("user") ;
if(commentUser!=null){
//有用户信息
//创建一个用户信息
UserInfo userInfo = new UserInfo() ;
userInfo.uid = commentUser.getString("id") ;
userInfo.name = commentUser.getString("screen_name") ;
userInfo.imageUrl = commentUser.getString("profile_image_url") ;
userInfo.bigUrl = commentUser.optString("avatar_large") ;
userInfo.description = commentUser.optString("description") ;
userInfo.favourites_count = commentUser.getInt("favourites_count") ;
userInfo.followers_count = commentUser.getInt("followers_count") ;
userInfo.friends_count = commentUser.getInt("friends_count") ;
userInfo.gender = commentUser.getString("gender") ;
userInfo.location = commentUser.getString("location") ;
userInfo.statuses_count = commentUser.getInt("statuses_count") ;
comment.userInfo = userInfo ;
}
JSONObject s = obj.getJSONObject("status") ;
if(s!=null){
//有微博信息
WeiBoInfo weiBoInfo = new WeiBoInfo() ;
weiBoInfo.id = s.getString("id") ;
weiBoInfo.time = Tookit.formatShowTime(s.getString("created_at")) ;
weiBoInfo.content = s.getString("text") ;
weiBoInfo.source = s.optString("source") ;
weiBoInfo.repost = s.getInt("reposts_count") ;
weiBoInfo.comment = s.getInt("comments_count") ;
weiBoInfo.imageUrl = s.optString("thumbnail_pic") ; //中等的缩略图
//转发微博信息
if(s.has("retweeted_status")){
Log.v("otcyan", "--------->retweeted_status") ;
JSONObject r=s.getJSONObject("retweeted_status");
RepeatWeiboInfo reWeibo = new RepeatWeiboInfo() ;
reWeibo.content = r.optString("text") ; //转发微博文字内容
reWeibo.imageUrl = r.optString("thumbnail_pic") ; //转发微博图片内容
weiBoInfo.repeatWeiboInfo = reWeibo ;
Log.v("otcyan", "测试转发:"+weiBoInfo.repeatWeiboInfo.content+"----------->imageUrl"+weiBoInfo.repeatWeiboInfo.imageUrl) ;
}
//表示有数据
JSONObject u = s.getJSONObject("user") ;
if(u!=null){
//创建一个用户信息
UserInfo userInfo = new UserInfo() ;
userInfo.uid = u.getString("id") ;
userInfo.name = u.getString("screen_name") ;
userInfo.imageUrl = u.getString("profile_image_url") ;
userInfo.bigUrl = u.optString("avatar_large") ;
userInfo.description = u.optString("description") ;
userInfo.favourites_count = u.getInt("favourites_count") ;
userInfo.followers_count = u.getInt("followers_count") ;
userInfo.friends_count = u.getInt("friends_count") ;
userInfo.gender = u.getString("gender") ;
userInfo.location = u.getString("location") ;
userInfo.statuses_count = u.getInt("statuses_count") ;
weiBoInfo.userInfo = userInfo ;
}
//加入
comment.wbInfo = weiBoInfo ;
}
//加入集合
comments.add(comment) ;
}
return comments ;
}
/**
* 获取 用户信息 包括 关注
* @param weibo
* @param params
* @param url
* @return
* @throws WeiboException
* @throws JSONException
*/
public List<UserInfo> getUserInfo(Weibo weibo , WeiboParameters params , String url) throws WeiboException, JSONException{
String info = weibo.request(context, url, params, Utility.HTTPMETHOD_GET,weibo.getAccessToken()) ;
//解析
JSONObject obj = new JSONObject(info) ;
List<UserInfo> userInfos = new ArrayList<UserInfo>() ;
JSONArray data = obj.getJSONArray("users") ;
for (int i = 0; i < data.length(); i++) {
UserInfo userInfo = new UserInfo() ;
JSONObject u = data.getJSONObject(i) ;
userInfo.uid = u.getString("id") ;
userInfo.name = u.getString("screen_name") ;
userInfo.imageUrl = u.getString("profile_image_url") ;
userInfo.bigUrl = u.optString("avatar_large") ;
userInfo.description = u.optString("description") ;
userInfo.favourites_count = u.getInt("favourites_count") ;
userInfo.followers_count = u.getInt("followers_count") ;
userInfo.friends_count = u.getInt("friends_count") ;
userInfo.gender = u.getString("gender") ;
userInfo.location = u.getString("location") ;
userInfo.statuses_count = u.getInt("statuses_count") ;
userInfos.add(userInfo) ;
}
return userInfos ;
}
/**
* 获取当前登录用户的关注id
* @param weibo
* @param params
* @param url
* @return
* @throws WeiboException
* @throws JSONException
*/
public String getCareId(Weibo weibo , WeiboParameters params , String url) throws WeiboException, JSONException{
String info = weibo.request(context, url, params, Utility.HTTPMETHOD_GET,weibo.getAccessToken()) ;
Log.v("otcyan", "关注id:"+info) ;
//解析
JSONObject obj = new JSONObject(info) ;
JSONArray data = obj.getJSONArray("ids") ;
return data.toString() ;
}
public List<Collect> getCollect(int page) throws WeiboException, JSONException{
Weibo weibo = Weibo.getInstance() ;
String url = Weibo.SERVER+"favorites.json" ;
WeiboParameters params = new WeiboParameters() ;
params.add("source", OtWeibo.CONSUMER_KEY) ;
params.add("count", page*100+"") ;
String info = weibo.request(context, url, params, Utility.HTTPMETHOD_GET, weibo.getAccessToken()) ;
Log.v("otcyan", "info:"+info) ;
JSONObject obj = new JSONObject(info) ;
JSONArray data = obj.getJSONArray("favorites") ;
List<Collect> collects = new ArrayList<Collect>() ;
for (int i = 0; i < data.length(); i++) {
JSONObject o = data.getJSONObject(i) ;
//创建 一个收藏
Collect c= new Collect() ;
JSONObject s = o.getJSONObject("status") ;
if(s!=null){
//有微博信息
WeiBoInfo weiBoInfo = new WeiBoInfo() ;
weiBoInfo.id = s.getString("id") ;
weiBoInfo.time = s.getString("created_at") ;
weiBoInfo.content = s.getString("text") ;
weiBoInfo.source = s.optString("source") ;
//weiBoInfo.repost = s.getInt("reposts_count") ;
//weiBoInfo.comment = s.getInt("comments_count") ;
weiBoInfo.imageUrl = s.optString("thumbnail_pic") ; //中等的缩略图
//转发微博信息
if(s.has("retweeted_status")){
Log.v("otcyan", "--------->retweeted_status") ;
JSONObject r=s.getJSONObject("retweeted_status");
RepeatWeiboInfo reWeibo = new RepeatWeiboInfo() ;
reWeibo.content = r.optString("text") ; //转发微博文字内容
reWeibo.imageUrl = r.optString("thumbnail_pic") ; //转发微博图片内容
weiBoInfo.repeatWeiboInfo = reWeibo ;
Log.v("otcyan", "测试转发:"+weiBoInfo.repeatWeiboInfo.content+"----------->imageUrl"+weiBoInfo.repeatWeiboInfo.imageUrl) ;
}
//表示有数据
if(s.has("user")){
JSONObject u = s.getJSONObject("user") ;
//创建一个用户信息
UserInfo userInfo = new UserInfo() ;
userInfo.uid = u.getString("id") ;
userInfo.name = u.getString("screen_name") ;
userInfo.imageUrl = u.getString("profile_image_url") ;
userInfo.bigUrl = u.optString("avatar_large") ;
userInfo.description = u.optString("description") ;
userInfo.favourites_count = u.getInt("favourites_count") ;
userInfo.followers_count = u.getInt("followers_count") ;
userInfo.friends_count = u.getInt("friends_count") ;
userInfo.gender = u.getString("gender") ;
userInfo.location = u.getString("location") ;
userInfo.statuses_count = u.getInt("statuses_count") ;
weiBoInfo.userInfo = userInfo ;
}
c.weiBoInfo = weiBoInfo ;
}
//加时间
c.time =Tookit.formatShowTime(o.optString("favorited_time")) ;
if(c.weiBoInfo!=null && c.weiBoInfo.userInfo!=null) //不等于 空加入到集合中
collects.add(c) ;
}
return collects ;
}
}