首页 > 代码库 > Android MaoZhuaWeiBo开发Service抓取个人信息-2
Android MaoZhuaWeiBo开发Service抓取个人信息-2
前面把主要的东西讲完了,之后就是数据的获取和解析显示出来了,那接下来我们就负责抓取数据的这块吧,首先我们需要
在清单文件里加载服务和活动
添加:、
<activity android:name="com.neweriweibo.activity.OAuthActivity"/> <activity android:name=".MainActivity"/> <activity android:name="com.neweriweibo.activity.SendMessageActivity"/> <!-- 获取自己微博信息 --> <service android:name="com.neweriweibo.service.UserService"/> <!-- 获取所有微博的信息 --> <service android:name="com.neweriweibo.service.WeiBoService"/>
下面看看用户个人信息的抓取:
package com.neweriweibo.service; /** * 个人信息数据抓取、 * @author Engineer-Jsp * @date 2014.10.28 * */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; import com.neweriweibo.model.User; import com.neweriweibo.utils.TencentAPI; import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.util.Log; public class UserService extends IntentService { public final static String UPDATA = http://www.mamicode.com/"com.neweriweibo.service.UPDATA";>
常规的网络请求抓取数据,将解析到的数据存在User对象里,并且在本地用BuffereWriter保存了一份,方便下登录,且考虑在没有网络的情况下或者数据抓取失败的情况下不至于界面因没有数据而显得空洞,不美观将获取的数据意图发送至应用程序,其他活动接收,并获取数据,显示在界面
User:
package com.neweriweibo.model; /** * 用户信息对象 * @author Engineer-Jsp * @date 2014.10.28 * */ import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable{ private String nick ;// 昵称 private String info ;// 自我介绍 private String loacation ;// 所在地 private String idonum ; //关注的人数 private String birthyeaer ;// 生日年 private String birthmonth ;// 生日月 private String birthday ;// 生日天 private String headimg;// 头像 private String fansnum;// 被关注数 private String tweetnum; // 发表的微博数 private String sex; // 性别 private String nearinfo; //用户最近的一条原创微博 public String getNearinfo() { return nearinfo; } public void setNearinfo(String nearinfo) { this.nearinfo = nearinfo; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getFansnum() { return fansnum; } public void setFansnum(String fansnum) { this.fansnum = fansnum; } public String getTweetnum() { return tweetnum; } public void setTweetnum(String tweetnum) { this.tweetnum = tweetnum; } public String getHeadimg() { return headimg; } public void setHeadimg(String headimg) { this.headimg = headimg; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getLoacation() { return loacation; } public void setLoacation(String loacation) { this.loacation = loacation; } public String getIdonum() { return idonum; } public void setIdonum(String idonum) { this.idonum = idonum; } public String getBirthyeaer() { return birthyeaer; } public void setBirthyeaer(String birthyeaer) { this.birthyeaer = birthyeaer; } public String getBirthmonth() { return birthmonth; } public void setBirthmonth(String birthmonth) { this.birthmonth = birthmonth; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public User(String nick, String info, String loacation, String idonum, String birthyeaer, String birthmonth, String birthday,String headimg, String fansnum , String tweetnum,String sex,String nearinfo) { super(); this.nick = nick; this.info = info; this.loacation = loacation; this.idonum = idonum; this.birthyeaer = birthyeaer; this.birthmonth = birthmonth; this.birthday = birthday; this.headimg = headimg; this.fansnum = fansnum; this.tweetnum = tweetnum; this.sex = sex; this.nearinfo = nearinfo; } public User() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "User [昵称=" + nick + ", 自我介绍=" + info + ", 城市=" + loacation + ", 关注数=" + idonum + ", 年=" + birthyeaer + ", 月=" + birthmonth + ", 日=" + birthday + ",头像地址="+headimg+",听众="+fansnum+",发表微博数="+tweetnum+",性别="+sex+ ",最近动态="+nearinfo+"]"; } public static final Parcelable.Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel source) { // TODO Auto-generated method stub User user = new User() ; user.nick = source.readString() ; user.info = source.readString() ; user.loacation = source.readString() ; user.idonum = source.readString() ; user.birthyeaer = source.readString() ; user.birthmonth = source.readString() ; user.birthday = source.readString() ; user.headimg = source.readString(); user.fansnum = source.readString(); user.tweetnum = source.readString(); user.sex = source.readString(); user.nearinfo = source.readString(); return user; } @Override public User[] newArray(int size) { // TODO Auto-generated method stub return new User[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(nick); dest.writeString(info); dest.writeString(loacation); dest.writeString(idonum); dest.writeString(birthyeaer); dest.writeString(birthmonth); dest.writeString(birthday); dest.writeString(headimg); dest.writeString(fansnum); dest.writeString(tweetnum); dest.writeString(sex); dest.writeString(nearinfo); } }
UserService extends IntentService 重写onHandleIntent(Intent intent) ,其实就相当于开启了一个子线程,来对网络请求进行处理,IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,这里就给我们提供了一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作Android MaoZhuaWeiBo开发Service抓取个人信息-2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。