標籤:des android style blog http io color ar os
前面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; @Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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") ; //擷取一個長整型的時間 :1003323String 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) ;}}}@Overridepublic 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>() {@Overridepublic UserWeibo createFromParcel(Parcel source) {// TODO Auto-generated method stubUserWeibo 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;}@Overridepublic UserWeibo[] newArray(int size) {// TODO Auto-generated method stubreturn new UserWeibo[size];}};@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.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;}@Overridepublic int describeContents() {return 0;}@Overridepublic 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>() {@Overridepublic 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;}@Overridepublic UserWeiBiInfo[] newArray(int size) {return new UserWeiBiInfo[size];}};@Overridepublic String toString() {return "UserWeiBiInfo [帳號=" + id + ", 暱稱=" + name + ", 微博訊息="+ origtext + ",頭像地址="+headimg+"]";}}
介面效果:
標題菜單:
轉寄、贊、評論功能都沒有拓展,有興趣的可以繼續做下去,跟著API寫就是了,不難~~
Android MaoZhuaWeiBo 社交動向更新資訊列表資料抓取 -3