原文出自:方杰|http://fangjie.info/?p=183转载请注明出处

最终效果演示:http://fangjie.info/?page_id=54

该项目代码已经放到github:https://github.com/JayFang1993/SinaWeibo

一.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;
    }
}

Android新浪微博客户端(五)——主界面的TabHost和WeiboUtil的更多相关文章

  1. android 新浪微博客户端的表情功能的实现

    这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...

  2. 实现 在子界面的button按下,在主界面的label显示。

    不知道理解的对不对,反正功能是实现了. 这是子界面,COM口配置界面的 .H文件的定义.下面的Private:定义了Ui:MainWindow  *main_ui;的指针变量      要   注  ...

  3. Android新浪微博客户端(六)——Home界面的ListView

    原文出自:方杰|http://fangjie.info/?p=184转载请注明出处 最终效果演示:http://fangjie.info/?page_id=54该项目代码已经放到github:http ...

  4. Android新浪微博客户端(一)——主框架搭建

    原文出自:方杰| http://fangjie.info/?p=62 转载请注明出处 提前声明的是,我是按照Ivan的这套教程学下来的. 首先,对于任何应用我们都需要建立一套消息处理机制,就是当用户在 ...

  5. Android新浪微博客户端(七)——ListView中的图片异步加载、缓存

    原文出自:方杰|http://fangjie.info/?p=193转载请注明出处 最终效果演示:http://fangjie.sinaapp.com/?page_id=54 该项目代码已经放到git ...

  6. Android新浪微博客户端(四)——添加多个账户及认证

    原文出自:方杰| http://fangjie.info/?p=75转载请注明出处 二.获取用户信息并保存数据库 上面说到加载AuthActivity有两种情况,其中一种就是授权成功回调,在授权回调成 ...

  7. Android新浪微博客户端(二)——添加多个账户及认证

    原文出自:方杰| http://fangjie.info/?p=69  转载请注明出处 先看下实现效果: 欢迎界面: 第一次进入登录界面登录由于在登录界面没有已授权用户信息,所以自动跳转到授权界面. ...

  8. Android新浪微博客户端(三)——添加多个账户及认证

    原文出自:方杰|http://fangjie.info/?p=72 转载请注明出处 一.微博OAuth2.0认证 首先来说说授权过程,我这里授权是通过SDK的,先添加SDK的jar包,微博SDK的de ...

  9. android 在fragment中获取界面的UI组件

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...

随机推荐

  1. [转] linux新的API signalfd、timerfd、eventfd使用说明

    http://blog.csdn.net/gdutliuyun827/article/details/8460417 三种新的fd加入linux内核的的版本: signalfd:2.6.22 time ...

  2. linux 一些笔记内容

    #which COMMAND : 显示命令路径#whatis COMMAND : 命令出现在哪个章节#type COMMAND :显示一个命令是内部命令还是外部命令#printenv :显示系统信息 ...

  3. Python下载漫画

    上午起来提不起劲,于是就用电脑看漫画,但是在线看漫画好烦,就想下下来看.一个一个点太麻烦,于是花了点时间用python写了个demo,把爱漫画的漫画下载下来,这样就可以随时随地看了.这也是我首次尝试用 ...

  4. ubuntu 连接 mssql <转>

    转自  http://www.sendong.net/thread-90941-1-1.html 在linux下连接MSSQL,因为微软同志没有提供任何接口给开发人员,大约他们认为要用MSSQL的,只 ...

  5. 转载:C#中的多态

    原文地址 http://www.cnblogs.com/jhxk/articles/1644018.html  感谢博主分享! 之前看到过类似的问题:如果面试时主考官要求你用一句话来描述多态,尽可能的 ...

  6. OD: Kernel Exploit - 2 Programming

    本节接前方,对 exploitme.sys 进行利用. exploitme.sys 存在任意地址写任意内容的内核漏洞,现在采用执行 Ring0 Shellcode 的方式进行利用. 获取 HalDis ...

  7. BestCoder 1st Anniversary

    Souvenir  Accepts: 1078  Submissions: 2366  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 26 ...

  8. iOS程序的完整启动过程(有storyboard)

    1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情:1> 创建UIApplication对象 2> ...

  9. iOS应用架构谈 开篇 (转)

    iOS应用架构谈 开篇  iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 网络层设计方案 iOS应用架构谈 动态部署方案 iOS应用架构谈 本地持久化方案 缘由 之前安居客iOS ap ...

  10. 31.Spring-开发流程.md

    [toc] 1.简单开发流程 1.1引用类库 基本类库: ## 1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.xml: <?xm ...