Android MaoZhuaWeiBo 好友动态信息列表数据抓取 -3

前面2篇把大致的开发说的差不多了,接下来说说粉丝动态消息列表或时间线数据的抓取与解析显示,我将他全部写在了一个

类里,并以封装类对象的形式存储数据,下面看看主要的服务代码:


粉丝动态消息列表数据抓取:

package com.neweriweibo.service;
/**
 * 用户消息列表
 * @author Engineer-Jsp
 * @date 2014.10.29
 * */
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.neweriweibo.model.UserWeibo;
import com.neweriweibo.utils.TencentAPI;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
public class WeiBoService extends Service {
	
    private ArrayList<UserWeibo> data ;
	private String access_token;
	private String openid;
	private String openkey;
	private String getURL; 
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		data = new ArrayList<UserWeibo>() ;
		// 首选项取数据
		access_token = PreferenceManager.getDefaultSharedPreferences(this).getString("access_token", "access_token");
		openid = PreferenceManager.getDefaultSharedPreferences(this).getString("openid", "openid");
		openkey = PreferenceManager.getDefaultSharedPreferences(this).getString("openkey", "openkey");
		// 拼接地址,赋值变量
		StringBuffer buff = new StringBuffer(TencentAPI.userWeiBo) ;
		buff.append("?").append("format=json").append("&pageflag=0")
		.append("&reqnum=5").append("&lastid=0").append("&type=3")
		.append("&contenttype=0x80").append("&oauth_consumer_key="+TencentAPI.client_id)
		.append("&access_token="+access_token)
		.append("&openid="+openid)
		.append("&clientip=CLIENTIP")
		.append("&oauth_version=2.a&scope=all");
		getURL = buff.toString();
		Log.d("获取微博消息列表地址:", getURL);
		new Thread(new Runnable() {
			@Override
			public void run() {
	/*
	 * http://open.t.qq.com/api/statuses/broadcast_timeline
	 * ?format=json
	 * &pageflag=0
	 * &pagetime=0
	 * &reqnum=5
	 * &lastid=0
	 * &type=3
	 * &contenttype=0x80
	 * &oauth_consumer_key=801506473
	 * &access_token=789a7d5284d0c3e608f8e384c993d04b
	 * &openid=0027BC08DB5B45D7461E9A0F16F527E7
	 * &clientip=CLIENTIP
	 * &oauth_version=2.a&scope=all 
	 * 
	 * 
	 */			
				try {
					getData(getURL) ;
			} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}).start()  ;
	}
	public void getData(String path)throws Exception{
		
		UserWeibo userweibo = null ;
		URL _url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
		conn.setConnectTimeout(8000);
		conn.setReadTimeout(8000);
		conn.setDoOutput(false);
		conn.setRequestMethod("GET");
		
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			InputStream in = conn.getInputStream();
			InputStreamReader reader = new InputStreamReader(in, "utf-8");
			BufferedReader buffere = new BufferedReader(reader);
			StringBuilder builder = new StringBuilder();
			String line = null;
			while (null != (line = buffere.readLine())) {
				builder.append(line);
			}
			String json = builder.toString() ;
			JSONObject  root = new JSONObject(json).getJSONObject("data") ;
			Log.d("获取到的微博消息列表:", root.toString());
			String totalnum = root.getString("totalnum") ;   //获取条数
			JSONArray arrayinfo = root.getJSONArray("info");
			Log.d("具体消息内容数据:", arrayinfo.toString());
			//获取广播的条数
			for(int i=0 ;i<arrayinfo.length() ;i++){
				
				userweibo = new UserWeibo() ;
				JSONObject object =  arrayinfo.getJSONObject(i) ;
				
				String date = object.getString("timestamp") ;   //获取一个长整型的时间 :1003323
				String from = object.getString("from") ;       //获取平台
				String location = object.getString("location") ;        //获取地址
				String name = object.getString("cfloat656805") ;        //获取名称
				String origtext = object.getString("origtext") ;          //获取内容
				
				
				userweibo.setForm(from) ; 
				userweibo.setLocation(location) ;
				userweibo.setOrigtext(origtext) ;
			}
		}	
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}


UserWeiBO:

package com.neweriweibo.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 用户个人的微博
 * @author Engineer-Jsp
 * @date 2014.10.28
 *
 */
public class UserWeibo  implements Parcelable{
	
	
	private String form ;   //从哪里发送的消息
	private String location ;  // 地址
	private String name ;  //名字
	private String origtext ;   //发送微博的信息
	private String timestamp ;  // 发表的时间
	private String number ;  //  广播的条数
	

	
	public UserWeibo() {
		super();
		// TODO Auto-generated constructor stub
	}
	public UserWeibo(String form, String location, String name,
			String origtext, String timestamp,String number ) {
		super();
		this.form = form;
		this.location = location;
		this.name = name;
		this.origtext = origtext;
		this.timestamp = timestamp;
		this.number = number ;
		
	}
	
	
	
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getForm() {
		return form;
	}
	public void setForm(String form) {
		this.form = form;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getOrigtext() {
		return origtext;
	}
	public void setOrigtext(String origtext) {
		this.origtext = origtext;
	}
	public String getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}
	
	
	public static final Parcelable.Creator<UserWeibo> CREATOR = new Parcelable.Creator<UserWeibo>() {

		@Override
		public UserWeibo createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			UserWeibo userweibo = new UserWeibo() ;
			userweibo.form = source.readString() ;
			userweibo.location = source.readString() ;
			userweibo.name = source.readString() ;
			userweibo.origtext = source.readString() ;
			userweibo.timestamp = source.readString() ;
			userweibo.number = source.readString() ;
			
			return userweibo;
		}

		@Override
		public UserWeibo[] newArray(int size) {
			// TODO Auto-generated method stub
			return new UserWeibo[size];
		}
	};
	
	
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
		dest.writeString(form);
		dest.writeString(location);
		dest.writeString(name);
		dest.writeString(origtext);
		dest.writeString(timestamp);
		dest.writeString(number) ;
		
	}
	

	
	
	  

}

UserWeiBoInfo:

package com.neweriweibo.model;
/**
 * 接收个人微博信息 
 * @author Engineer-Jsp
 * @date 2014.10.28
 */
import android.os.Parcel;
import android.os.Parcelable;
public class UserWeiBiInfo  implements Parcelable{
	
	 private String  id ;  //微博id
	 private String  name ;  //微博人的名字
	 private String  origtext ;  //发送的信息
	 private String  headimg;//头像

	public String getHeadimg() {
		return headimg;
	}
	public void setHeadimg(String headimg) {
		this.headimg = headimg;
	}
	public UserWeiBiInfo() {
		super();
	}
	public UserWeiBiInfo(String id, String name, String origtext,String headimg) {
		super();
		this.id = id;
		this.name = name;
		this.origtext = origtext;
		this.headimg = headimg;
	}
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getOrigtext() {
		return origtext;
	}
	
	
	public void setOrigtext(String origtext) {
		this.origtext = origtext;
	}
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		
		dest.writeString(id);
		dest.writeString(name);
		dest.writeString(origtext);
		dest.writeString(headimg);		
	}
	
	 public static final Parcelable.Creator<UserWeiBiInfo> CREATOR = new Creator<UserWeiBiInfo>() {

		@Override
		public UserWeiBiInfo createFromParcel(Parcel source) {
			UserWeiBiInfo userWeiBiInfo = new UserWeiBiInfo() ;
			
			userWeiBiInfo.id = source.readString() ;
			userWeiBiInfo.name = source.readString() ;
			userWeiBiInfo.origtext = source.readString() ;
			userWeiBiInfo.headimg = source.readString();
			
			return userWeiBiInfo;
		}

		@Override
		public UserWeiBiInfo[] newArray(int size) {
			return new UserWeiBiInfo[size];
		}
	};
	
	@Override
	public String toString() {
		return "UserWeiBiInfo [帐号=" + id + ", 昵称=" + name + ", 微博消息="
				+ origtext + ",头像地址="+headimg+"]";
	}
}

界面效果:



标题菜单:



转发、赞、评论功能都没有拓展,有兴趣的可以继续做下去,跟着API写就是了,不难~~

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。