一.TabHost的實現 之前的一篇文章講的就是TabHost,但是那個是用Fragment實現TabHost,這裡我就嘗試用另一種方式,繼承TabActivity的方式實現TabHost。MainActivity.java 複製代碼public class MainActivity extends TabActivity{ private TabHost tabHost; private static final String HOME_TAB="home"; private 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; } } }); }}複製代碼布局檔案: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:id="@android:id/tabhost"<!--注意這裡 --> android:background="#ffffff" > <TabWidget android:id="@android:id/tabs"<!--注意這裡 --> android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone"/> <FrameLayout android:id="@android:id/tabcontent"<!--注意這裡 --> 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="首頁" /> <RadioButton android:id="@+id/rb_at" android:drawableTop="@drawable/at_icon" style="@style/rg_style" android:text="我" /> <RadioButton android:id="@+id/rb_mess" android:drawableTop="@drawable/msg_icon" style="@style/rg_style" android:text="訊息" /> <RadioButton android:id="@+id/rb_more" android:drawableTop="@drawable/more_icon" style="@style/rg_style" android:text="更多" /> </RadioGroup></TabHost>複製代碼 其中的每一個Tab選項的內容都是一個Activity,然後再去針對性的寫每一個Tab下的內容。 二.WeiboAPI WeiboUtil.java 這個類的操作主要是發Http請求,然後解析資料,封裝到bean中。這裡我唯寫好了 讀取所關注人的微博列表。下面的HttpRequest是我寫的一個HTTP請求的方法,因為經常涉及到Http請求,就把這部分代碼抽出來寫成一個方法。 WeiboUtil.java的全部代碼 複製代碼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")); else weibo.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")); else zweibo.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請求 way-0 Post way-1 Get public 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) { // 開啟和URL之間的串連 connection= realUrl.openConnection(); // 發送POST請求必須設定如下兩行 connection.setDoOutput(true); connection.setDoInput(true); // 擷取URLConnection對象對應的輸出資料流 out = new PrintWriter(connection.getOutputStream()); // 發送請求參數 out.print(params); // flush輸出資料流的緩衝 out.flush(); } else { // 開啟和URL之間的串連 connection = realUrl.openConnection(); // 設定通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的串連 //connection.connect(); } // 定義 BufferedReader輸入資料流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入資料流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }}