使用HttpUTILS请求XML数据

来源:互联网 发布:户外广告行业数据 编辑:程序博客网 时间:2024/06/05 21:11
public class BKFragments extends Fragment implements IXListViewListener{
    private XListView lv;
    private HttpUtils httpUtils;
    private int num=0;
    private List<BlogBean> list;
    private Handler handler;
    private boolean flag;
    private boolean tag;
    private BKAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_selection_common,
                container, false);

        lv = (XListView) rootView.findViewById(R.id.lv);

        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        lv.setPullLoadEnable(true);
        httpUtils = new HttpUtils();

        // 获取网络数据
        startHttpUtils(num);

        // 设置监听
        lv.setXListViewListener(this);

        handler = new Handler();
        
    }

    private void startHttpUtils(int id) {
        String path="http://www.oschina.net/action/api/blog_list?type=latest&pageIndex="+id+"&pageSize=20";
         httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

                

                @Override
                public void onFailure(HttpException arg0, String arg1) {
                   System.out.println("请求失败");
                }

                @Override
                public void onSuccess(ResponseInfo<String> weResponseInfo) {

                    String data = weResponseInfo.result;
                    List<BlogBean>   lists = new PullUtils().getBoKelist(data);
                   System.out.println(lists);

                   if(flag==true){
                       System.out.println("头部加载");
                       list.addAll(0, lists);
                   }else{
                       if(tag==false){
                           System.out.println("首次加载");
                           tag=true;
                           list=lists;
                       }else{
                           System.out.println("尾部加载");
                           list.addAll(lists);
                       }
                   }
                   setData();

                }

            });
    }

    
    protected void setData() {
         adapter = new BKAdapter(getActivity(),list);

        lv.setAdapter(adapter);

    }
    
     private void onLoad() {
            lv.stopRefresh();
            lv.stopLoadMore();
            lv.setRefreshTime("刚刚");
        }

    @Override
    public void onRefresh() {
         handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    flag=true;
                    num +=1;
                    startHttpUtils(num);
                   
                    adapter.notifyDataSetChanged();
                    onLoad();

                }
            }, 2000);
    }

    @Override
    public void onLoadMore() {
         handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    flag=false;
                    num +=1;
                    startHttpUtils(num);
                    adapter.notifyDataSetChanged();
                    onLoad();

                }
            }, 2000);
    }
}
0 0