Android Maozhuaweibo Development Service Crawl personal information-2

Source: Internet
Author: User
Tags openid

Before the main things are finished, after that is the data acquisition and analysis of the show, then we are responsible for capturing the data this piece, first we need to

in the inventory file contains services and activities


Add to:

<activity android:name= "com.neweriweibo.activity.OAuthActivity"/>        <activity android:name= ". Mainactivity "/>        <activity android:name=" com.neweriweibo.activity.SendMessageActivity "/>                <!- -Get your own Weibo information--        <service android:name= "Com.neweriweibo.service.UserService"/>                <!--get information about all Weibo- >        <service android:name= "Com.neweriweibo.service.WeiBoService"/>

Here's a look at the user's personal information crawl:

Package com.neweriweibo.service;/** * Personal information data capture, * @author engineer-jsp * @date 2014.10.28 * */import Java.io.BufferedReade R;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 = "Com.neweriweibo.ser Vice. Updata ";p ublic final static String Now_weather =" Userweibo ";p rivate stringbuffer _buff;private File weibousercurrentfil E;private bufferedwriter write;private bufferedwriter writefour;private static BufferedReader reader;p rivate String Access_token;private string Openid;private string Openkey;public UserService () {Super ("WeatherService");} @Overridepublic void OnCreate () {super.oncreate (); weibousercurrentfile = new File ( Environment.getexternalstoragedirectory (), "Weiinfo"), if (!weibousercurrentfile.exists ()) {try { Weibousercurrentfile.createnewfile ();//create} catch (IOException e) {e.printstacktrace ();}} Access_token = Preferencemanager.getdefaultsharedpreferences (this). GetString ("Access_token", "Access_token"); o Penid = Preferencemanager.getdefaultsharedpreferences (this). GetString ("OpenID", "OpenID"); Openkey = Preferencemanager.getdefaultsharedpreferences (This). GetString ("Openkey", "Openkey");} @Overridepublic void OnDestroy () {Super.ondestroy ();} /** * Https://open.t.qq.com/api/user/info * oauth_consumer_key=801506473 * &access_token= 789A7D5284D0C3E608F8E384C993D04B * &openid=0027bc08db5b45d7461e9a0f16f527e7 * &clientip=CLIENTIP&oauth _version=2.a&scope=all */@Overrideprotected void onhandleintent (inTent Intent) {//oauth_consumer_key=801506545&access_token=c63ed52ee874eff6e61dfe66d2c7b396&openid= 0027bc08db5b45d7461e9a0f16f527e7&clientip=clientip&oauth_version=2.a&scope=all//Oauth_consumer_key =xx&access_token=xx&openid=xx&clientip=xx&oauth_version=2.a&scope=xxuser weibouser = new User ( ); StringBuffer buff = new StringBuffer (tencentapi.userinfo); Buff.append ("?"). Append ("oauth_consumer_key=" +tencentapi.client_id). Append ("&access_token=" +access_token). Append ("& Openid= "+openid"). Append ("&clientip=clientip&oauth_version=2.a&scope=all");   LOG.I ("Get the request address of the current login account personal information:", buff.tostring () + ""); try {URL _url = new URL (buff.tostring ()); 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 (); LOG.I ("Get data for current login account personal information:", json.tostring () + "\ n");//parse data jsonobject root = new Jsonobject (JSON). Getjsonobject ("Data") ; Jsonarray tweetinfo = Root.getjsonarray ("Tweetinfo"); LOG.D ("The user's latest original Weibo:", tweetinfo.tostring ()); Jsonobject zero = tweetinfo.getjsonobject (0); String nearinfo = zero.getstring ("text"); LOG.D ("Original Weibo information:", nearinfo.tostring ()); String Nick = root.getstring ("Nick"); String idolnum = root.getstring ("Idolnum"); String location = root.getstring ("location"); String brithyear = root.getstring ("Birth_year"); String brithmonth = root.getstring ("Birth_month"); String brithday = root.getstring ("Birth_day"); String introduction = root.getstring ("Introduction"); String headimg = root.getstring ("head") + "/40"; StrinG Fansnum = root.getstring ("Fansnum");                 String tweetnum = root.getstring ("Tweetnum");                 String sex = root.getstring ("Sex");                 LOG.D ("Sex content test, just obtained:", sex);                 if (Sex.equals ("1")) {sex = "male";                 }else if (Sex.equals ("2")) {sex = "female";                 }else{sex = "";                } log.d ("Sex content test, after judgment:", sex); Write = new BufferedWriter (new FileWriter (Weibousercurrentfile)) Write.write (Nick + "*" + idolnum + "*" + location+ "*" + brithyear+ "/" +brithmonth+ "/" +brithday+ "*" +introduction+ "*" +headimg+ "*" +fansnum+ "*" +tweetnum+ "*" +sex+ "*" +  Nearinfo); Write.close (); Weibouser.setnick (Nick); Weibouser.setidonum (Idolnum); weibouser.setloacation (location); Weibouser.setbirthyeaer (  Brithyear); Weibouser.setbirthmonth (Brithmonth); Weibouser.setbirthday (brithday); Weibouser.setheadimg (headimg); Weibouser.setinfo (introduction); Weibouser.setfansnum (fansnum); weIbouser.settweetnum (Tweetnum); weibouser.setsex (sex); Weibouser.setnearinfo (Nearinfo); Conn.disconnect ();}}    catch (Exception e) {e.printstacktrace ();} Intent _intent = new Intent (updata); Bundle Mbundle = new bundle (); Mbundle.putparcelable (Userservice.now_weather, Weibouser); _intent.putextras (MBundle); Sendbroadcast (_intent);}}

The regular network request crawls the data, will parse the data to exist in the user object, and in the local use Bufferewriter to save a copy, facilitates the log-in, and considers that without the network situation or the data fetching failure situation does not have the interface because does not have the data to appear empty, is not beautiful

The data is intended to be sent to the application, other activities receive, and the data is retrieved, displayed in the interface

User:

Package com.neweriweibo.model;/** * User Information Object * @author engineer-jsp * @date 2014.10.28 * */import Android.os.parcel;import A Ndroid.os.parcelable;public class User implements Parcelable{private string Nick;//nickname private String info;//Self introduction privat  E string loacation;//location private String idonum; Number of followers private string Birthyeaer;//Birthday of the private string birthmonth;//Birthday month private string birthday;//Birthday Day private String Hea dimg;//Avatar Private String fansnum;//is concerned with the number of private string tweetnum; Number of Weibo published private String sex; Sex private String nearinfo; User's latest original Weibo 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 Birthda Y,string headimg,stringFansnum, String tweetnum,string sex,string nearinfo) {super (); This.nick = Nick;this.info = Info;this.loacation = Loacatio N;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} @Overridepublic String toString () {return "User [nickname =" + Nic K + ", Self introduction =" + Info + ", city =" + Loacation + ", number of followers =" + Idonum + ", year =" + Birthyeaer + ", month =" + Birthmonth + ", day =" + Birthda " Y + ", Avatar address =" +headimg+ ", audience =" +fansnum+ ", published microblog number =" +tweetnum+ ", gender =" +sex+ ", recent dynamic =" +nearinfo+ "]";} public static final parcelable.creator<user> Creator = new creator<user> () {@Overridepublic User  Createfromparcel (Parcel source) {//TODO auto-generated method Stubuser 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;} @Overridepublic user[] NewArray (int size) {//TODO auto-generated method Stubreturn new User[size];}}; @Overridepublic int describecontents () {//TODO auto-generated method Stubreturn 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {//TODO auto-generated Method Stubdest.writestring (Nick); Dest.writestring (Info);d est.writestring (loacation);d est.writestring (idonum);d est.writestring (Birthyeaer); Dest.writestring (Birthmonth);d est.writestring (birthday);d est.writestring (headimg);d est.writestring (fansnum); Dest.writestring (Tweetnum);d est.writestring (Sex);d est.writestring (Nearinfo);}}

UserService extends Intentservice rewrite onhandleintent (Intent Intent) is actually equivalent to opening a sub-thread to process the network request,Intentservice uses the queue to join the requested intent to the queue, then opens a worker thread to handle intent in the queue, and for asynchronous StartService requests, Intentservice will process the completion of a second after processing, each request will be processed in a separate worker thread, will not block the application's main thread, here gives us a thought, It is better to use Intentservice to handle time-consuming operations if there is a time-consuming operation than opening a new thread within the service

Android Maozhuaweibo Development Service Crawl personal information-2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.