Original from: Miroslav | http ://fangjie.sinaapp.com/?p=183   reprint please specify the source
Final Effect Demo: http://fangjie.sinaapp.com/?page_id=54
The project code has been put into Github:https://github.com/jayfang1993/sinaweibo
I. Implementation of the Tabhost
One of the previous articles is about Tabhost, but that is the implementation of tabhost with fragment, and here I try to implement tabhost in a different way, inheriting tabactivity.
Mainactivity.java
public class Mainactivity extends Tabactivity{private tabhost tabhost;private static final String home_tab= "HOME"; privat E static final String at_tab= "at"; private static final String msg_tab= "MSG"; private static final String more_tab= "more"; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Tabhost = This.gettabhost (); Tabspec Homespec=tabhost.newtabspec (Home_tab). Setindicator (Home_tab). SetContent (New Intent (This, Homeactivity.class)); Tabspec Atspec=tabhost.newtabspec (At_tab). Setindicator (At_tab). SetContent (New Intent (This,atactivity.class)); Tabspec Msgspec=tabhost.newtabspec (Msg_tab). Setindicator (Msg_tab). SetContent (New Intent (This,msgactivity.class)) ; Tabspec Morespec=tabhost.newtabspec (More_tab). Setindicator (More_tab). SetContent (New Intent (This, Moreactivity.class)); Tabhost.addtab (HOMESPEC); Tabhost.addtab (ATSPEC); Tabhost.addtab (MSGSPEC); Tabhost.addtab (MORESPEC); Tabhost.setcurrentTabbytag (Home_tab); Radiogroup Radiogroup = (radiogroup) This.findviewbyid (R.id.main_radio); Final RadioButton rb= (RadioButton) This.findviewbyid (r.id.rb_home); Rb.setbackgroundresource (r.drawable.tabhost_press); Radiogroup.setoncheckedchangelistener (New Oncheckedchangelistener () {public void oncheckedchanged (RadioGroup Group, int checkedid) {rb.setbackgroundresource (r.drawable.tabhost_bg_selector); switch (checkedid) {case R.id.rb_home: Tabhost.setcurrenttabbytag (home_tab); Break;case R.id.rb_at:tabhost.setcurrenttabbytag (AT_TAB); Break;case R.id.rb _mess:tabhost.setcurrenttabbytag (msg_tab); Break;case R.id.rb_more:tabhost.setcurrenttabbytag (MORE_TAB); break; Default:break;}});}}
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android "Android:orientation=" Horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "Android Oid:id= "@android: Id/tabhost" <!--note--android:background= "#ffffff" ><tabwidget android:id= "@andro Id:id/tabs "<!--note--android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:vi Sibility= "Gone"/><framelayout android:id= "@android: Id/tabcontent" <!--note here--android:layout_width= " Fill_parent "android:layout_height=" fill_parent "/><radiogroup android:id=" @+id/main_radio "Android:layout_ Width= "Fill_parent" android:layout_height= "48DP" android:layout_gravity= "bottom" android:orientation= "horizontal" android:background= "@drawable/bar" > <radiobutton android:id= "@+id/rb_home" android:drawabletop = "@drawable/home_icon" style= "@sTyle/rg_style "android:text=" Home "/> <radiobutton android:id=" @+id/rb_at "android:drawabletop = "@drawable/at_icon" style= "@style/rg_style" android:text= "i"/> <radiobutton android:id= "@+ Id/rb_mess "android:drawabletop=" @drawable/msg_icon "style=" @style/rg_style "android:text=" message " /> <radiobutton android:id= "@+id/rb_more" android:drawabletop= "@drawable/more_icon" St yle= "@style/rg_style" android:text= "more"/> </RadioGroup></TabHost>
The content of each tab option is an activity, and then the content of each tab is written accordingly.
Two. Weiboapi Weiboutil.java
The main operation of this class is to send an HTTP request, then parse the data and encapsulate it in the bean. Here I've only written a list of tweets that read the people you care about. The following HttpRequest is a method of an HTTP request that I write, because it often involves HTTP requests, and it pulls this part of the code out to write a method.
Weiboutil.java all the Code
Package Com.fangjie.weibo.util;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.net.url;import Java.net.urlconnection;import Java.util.arraylist;import Java.util.list;import Org.json.jsonarray;import Org.json.jsonexception;import Org.json.JSONObject;import Com.fangjie.weibo.bean.user;import Com.fangjie.weibo.bean.weibo;public class Weiboutil {public List<Weibo> Getweibolist (String token, long i) {list<weibo> weibos=new arraylist<weibo> (); String url= "Https://api.weibo.com/2/statuses/home_timeline.json"; String params= "access_token=" +token+ "&max_id=" +i; String result=httprequest (1, URL, params); try {jsonobject json_res=new jsonobject (result); Jsonarray Json_weibos=json_res.getjsonarray ("statuses"); for (int j=0;j<json_weibos.length (); j + +) {Weibo weibo= New Weibo (); Jsonobject Json_weibo=json_weibos.getjsonobject (j); Weibo.setcomments_count (Json_weibo.getint ("Comments_count")) ; Weibo.setcontent (json_weibo.getstring ("text"); Weibo.setfrom (json_weibo.getstring ("source")); Weibo.setreposts_count (Json_weibo.getint ("Reposts_count")); Weibo.settime (json_weibo.getstring ("Created_at")); Weibo.setwid (Json_weibo.getlong ("id")); User User=new user (); Jsonobject json_user=json_weibo.getjsonobject ("user"); User.setuid (Json_user.getlong ("id")); User.setname (Json_ User.getstring ("Screen_name")); User.setprofile_image_url (json_user.getstring ("Profile_image_url")); USER.SETISV (Json_user.getboolean ("verified")), Weibo.setuser (user), if (!json_weibo.isnull ("Thumbnail_pic")) Weibo.setbmiddle _pic (json_weibo.getstring ("Thumbnail_pic")); Elseweibo.setbmiddle_pic (""); if (!json_weibo.isnull ("Retweeted_ Status ") {Weibo zweibo=new Weibo (); Jsonobject json_zweibo=json_weibo.getjsonobject ("Retweeted_status"); Zweibo.setcomments_count (json_zweibo.getInt ("Comments_count")); Zweibo.setcontent (json_zweibo.getstring ("text")); Zweibo.setfrom (json_zweibo.getstring ("source")); Zweibo.setreposts_count (Json_zweibo.getint ("Reposts_count")); Zweibo.settime (Json_zweIbo.getstring ("Created_at")); Zweibo.setwid (Json_zweibo.getlong ("id")); User Zuser=new user (); Jsonobject json_zuser=json_zweibo.getjsonobject ("user"); Zuser.setuid (Json_zuser.getlong ("id")); Zuser.setName ( Json_zuser.getstring ("Screen_name")); Zuser.setprofile_image_url (json_zuser.getstring ("Profile_image_url")); ZUSER.SETISV (Json_zuser.getboolean ("verified")); Zweibo.setuser (Zuser); if (!json_zweibo.isnull ("Thumbnail_pic")) Zweibo.setbmiddle_pic (json_zweibo.getstring ("Thumbnail_pic")); Elsezweibo.setbmiddle_pic (""); Weibo.setWeibo ( Zweibo);} Else{weibo.setweibo (null);} Weibos.add (Weibo); System.out.println (weibo.content);}} catch (Jsonexception e) {e.printstacktrace ();} return Weibos;} HTTP request way-0 Post way-1 getpublic static string HttpRequest (int way,string url,string params) {string result = ""; BufferedReader in = null; PrintWriter out = null; try {String urlnamestring = URL + "?" + params; System.out.println (urlnamestring); URL realurl = new URL (urlnamestring); URLConnection connection; if (way==0) {//Open and URL connection connection= realurl.openconnection (); The Send POST request must be set to the following two lines connection.setdooutput (true); Connection.setdoinput (TRUE); Gets the output stream corresponding to the URLConnection object out = new PrintWriter (Connection.getoutputstream ()); Send request parameter out.print (params); Flush output Stream Buffer Out.flush (); } else {//Open and URL connection connection = Realurl.openconnection (); Set the generic request attribute Connection.setrequestproperty ("accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); Establish an actual connection Connection.connect (); }//define BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (connection.getinput Stream ())); String Line; while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {System.out.println ("Send GET request exception!) "+ e); E.printstacktrace (); }//Use finally block to close the input stream finally {try {if (in! = null) {In.clos E (); }} catch (Exception E2) {e2.printstacktrace (); }} return result;}}Welcome to the attention of my personal site:http://fangjie.sinaapp.com/