Xml pull解析,XUtils网络请求

本文介绍如何使用Xutils发起网络请求获取XML数据,并通过解析这些数据填充到四个不同的Fragment中,每个Fragment对应一个新闻类别:资讯、热点、博客、推荐。文章详细展示了XML解析过程及XListView的使用,包括下拉刷新和上拉加载更多功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用radioGroup和radioButton实现四个选项卡,“资讯”、“热点”、“博客”、“推荐”

使用Xutils方式实现请求网络接口

解析XML数据,封装到集合里面,用XListView显示解析数据,实现下拉刷新,上拉加载,  需要添加XListView依赖包

[html]  view plain  copy
  1. <span style="font-size:18px;">activity_main:  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <RadioGroup  
  10.         android:id="@+id/radioGroup1"  
  11.         android:layout_width="match_parent"  
  12.         android:orientation="horizontal"  
  13.         android:layout_height="40dp"  
  14.          >  
  15.   
  16.         <RadioButton  
  17.             android:id="@+id/radio0"  
  18.             android:button="@null"  
  19.             android:layout_weight="1"  
  20.             android:layout_width="0dp"  
  21.             android:gravity="center"  
  22.             android:layout_height="fill_parent"  
  23.             android:textColor="#00ff00"  
  24.             android:textSize="20sp"  
  25.             android:text="资讯" />  
  26.   
  27.         <RadioButton  
  28.             android:id="@+id/radio1"  
  29.             android:button="@null"  
  30.             android:textSize="20sp"  
  31.             android:layout_weight="1"  
  32.             android:layout_width="0dp"  
  33.             android:gravity="center"  
  34.             android:layout_height="fill_parent"  
  35.             android:text="热点" />  
  36.   
  37.         <RadioButton  
  38.             android:id="@+id/radio2"  
  39.             android:button="@null"  
  40.             android:textSize="20sp"  
  41.             android:layout_weight="1"  
  42.             android:layout_width="0dp"  
  43.             android:gravity="center"  
  44.             android:layout_height="fill_parent"  
  45.             android:text="博客" />  
  46.           
  47.         <RadioButton  
  48.             android:id="@+id/radio3"  
  49.             android:button="@null"  
  50.             android:textSize="20sp"  
  51.             android:layout_weight="1"  
  52.             android:layout_width="0dp"  
  53.             android:gravity="center"  
  54.             android:layout_height="fill_parent"  
  55.             android:text="推荐" />  
  56.     </RadioGroup>  
  57.     <android.support.v4.view.ViewPager  
  58.         android:layout_width="match_parent"  
  59.         android:layout_height="wrap_content"  
  60.         android:id="@+id/viewPager"  
  61.         />  
  62.   
  63. </LinearLayout></span>  


fragment.xml代码

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.      <me.maxwin.view.XListView  
  7.          android:layout_width="match_parent"  
  8.          android:layout_height="wrap_content"  
  9.          android:id="@+id/xListView1"  
  10.          ></me.maxwin.view.XListView>  
  11.   
  12. </LinearLayout>  

Bean

[html]  view plain  copy
  1. package com.bwie.test2.bean;  
  2.   
  3. public class Zixun {  
  4.     private String title;  
  5.     private String body;  
  6.     private String pubDate;  
  7.     private String author;  
  8.     public String getTitle() {  
  9.         return title;  
  10.     }  
  11.     public void setTitle(String title) {  
  12.         this.title = title;  
  13.     }  
  14.     public String getBody() {  
  15.         return body;  
  16.     }  
  17.     public void setBody(String body) {  
  18.         this.body = body;  
  19.     }  
  20.     public String getPubDate() {  
  21.         return pubDate;  
  22.     }  
  23.     public void setPubDate(String pubDate) {  
  24.         this.pubDate = pubDate;  
  25.     }  
  26.     public String getAuthor() {  
  27.         return author;  
  28.     }  
  29.     public void setAuthor(String author) {  
  30.         this.author = author;  
  31.     }  
  32.     public Zixun(String title, String body, String pubDate, String author) {  
  33.         super();  
  34.         this.title = title;  
  35.         this.body = body;  
  36.         this.pubDate = pubDate;  
  37.         this.author = author;  
  38.     }  
  39.     public Zixun() {  
  40.         super();  
  41.         // TODO Auto-generated constructor stub  
  42.     }  
  43.     @Override  
  44.     public String toString() {  
  45.         return "Zixun [title=" + title + "body=" + body + "pubDate="  
  46.                 + pubDate + ", author=" + author + "]";  
  47.     }  
  48.       
  49. }  

接下来是Fragment.java中的代码,一共有四个Fragment,代码一样,唯一区别是请求数据的地址不同

[html]  view plain  copy
  1. package com.bwie.test2.fragment;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.util.ArrayList;  
  5.   
  6. import org.xmlpull.v1.XmlPullParser;  
  7. import org.xmlpull.v1.XmlPullParserException;  
  8.   
  9. import me.maxwin.view.XListView;  
  10. import me.maxwin.view.XListView.IXListViewListener;  
  11.   
  12. import com.bwie.test2.R;  
  13. import com.bwie.test2.adapter.MyBaseAdapter;  
  14. import com.bwie.test2.bean.Zixun;  
  15. import com.lidroid.xutils.HttpUtils;  
  16. import com.lidroid.xutils.exception.HttpException;  
  17. import com.lidroid.xutils.http.ResponseInfo;  
  18. import com.lidroid.xutils.http.callback.RequestCallBack;  
  19. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;  
  20.   
  21. import android.os.Bundle;  
  22. import android.support.v4.app.Fragment;  
  23. import android.util.Xml;  
  24. import android.view.LayoutInflater;  
  25. import android.view.View;  
  26. import android.view.ViewGroup;  
  27. import android.widget.TextView;  
  28.   
  29. public class Fragment1 extends Fragment <span style="color:#FF0000;">implements IXListViewListener</span>{  
  30.     private XListView xlistView;  
  31.     boolean isRefresh = false;  
  32.     ArrayList<Zixun> mZxList=new ArrayList<Zixun>();  
  33.     @Override  
  34.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  35.             Bundle savedInstanceState) {  
  36.         View view = inflater.inflate(R.layout.fragment1, container, false);  
  37.         xlistView = (XListView) view.findViewById(R.id.xListView1);  
  38.         return view;  
  39.     }  
  40.     @Override  
  41.     public void onActivityCreated(Bundle savedInstanceState) {  
  42.         super.onActivityCreated(savedInstanceState);  
  43.         xlistView.setPullLoadEnable(true);  
  44.         xlistView.setXListViewListener(this);  
  45.         myBaseAdapter=null;  
  46.         questNet();  
  47.     }  
  48.     private int index=0;  
  49.     private Zixun zixun;  
  50.     private MyBaseAdapter myBaseAdapter;  
  51.     private void questNet() {  
  52.         String path="http://www.oschina.net/action/api/news_list?pageIndex="+index;  
  53.         HttpUtils httpUtils = new HttpUtils();  
  54.         httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {  
  55.   
  56.               
  57.   
  58.             @Override  
  59.             public void onFailure(HttpException arg0, String arg1) {  
  60.                   
  61.             }  
  62.   
  63.             @Override  
  64.             public void onSuccess(ResponseInfo<String> arg0) {  
  65.                 String result = arg0.result;  
  66.                 ByteArrayInputStream arrayInputStream=new ByteArrayInputStream(result.getBytes());  
  67.                 ArrayList<Zixun> startAnalyze = startAnalyze(arrayInputStream);  
  68.                 if (isRefresh) {  
  69.                     mZxList.clear();  
  70.                 }  
  71.                 mZxList.addAll(startAnalyze);  
  72.                 if (myBaseAdapter == null) {  
  73.                     myBaseAdapter = new MyBaseAdapter(getActivity(), mZxList);  
  74.                     xlistView.setAdapter(myBaseAdapter);  
  75.                 } else {  
  76.                     myBaseAdapter.notifyDataSetChanged();  
  77.                 }  
  78.                 onLoad();  
  79.             }  
  80.         });  
  81.     }  
  82.     protected ArrayList<Zixun> startAnalyze(ByteArrayInputStream arrayInputStream) {  
  83.         ArrayList<Zixun> zxList = new ArrayList<Zixun>();  
  84.         try {  
  85.             XmlPullParser newPullParser = Xml.newPullParser();  
  86.             newPullParser.setInput(arrayInputStream, "utf-8");  
  87.             int eventType = newPullParser.getEventType();  
  88.             while (eventType != XmlPullParser.END_DOCUMENT) {  
  89.                 String name = newPullParser.getName();  
  90.                 switch (eventType) {  
  91.                 case XmlPullParser.START_TAG:  
  92.                     if ("news".equals(name)) {  
  93.                         zixun = new Zixun();  
  94.                     } else if ("title".equals(name)) {  
  95.                         zixun.setTitle(newPullParser.nextText());  
  96.                     } else if ("body".equals(name)) {  
  97.                         zixun.setBody(newPullParser.nextText());  
  98.                     } else if ("pubDate".equals(name)) {  
  99.                         zixun.setPubDate(newPullParser.nextText());  
  100.                     } else if ("author".equals(name)) {  
  101.                         zixun.setAuthor(newPullParser.nextText());  
  102.                     }   
  103.                     break;  
  104.   
  105.                 case XmlPullParser.END_TAG:  
  106.                     if ("news".equals(name)) {  
  107.                         zxList.add(zixun);  
  108.                         zixun = null;  
  109.                     }  
  110.                     break;  
  111.   
  112.                 default:  
  113.                     break;  
  114.                 }  
  115.                 eventType = newPullParser.next();  
  116.             }  
  117.         } catch (Exception e) {  
  118.             // TODO Auto-generated catch block  
  119.             e.printStackTrace();  
  120.         }  
  121.         return zxList;  
  122.     }  
  123.     @Override  
  124.     public void onRefresh() {  
  125.         index = 0;  
  126.         isRefresh = true;  
  127.     }  
  128.     @Override  
  129.     public void onLoadMore() {  
  130.         index++;  
  131.         isRefresh = false;  
  132.         // 重新请求  
  133.         questNet();  
  134.     }  
  135.     private void onLoad() {  
  136.         xlistView.stopRefresh();  
  137.         xlistView.stopLoadMore();  
  138.         xlistView.setRefreshTime("刚刚");  
  139.     }  
  140. }  
fragment1_item.xml布局,XListView的布局

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView   
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:textSize="20sp"  
  10.         android:padding="10dp"  
  11.         android:textStyle="bold"  
  12.         android:id="@+id/tv_title"  
  13.         />  
  14.     <TextView   
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:id="@+id/tv_body"  
  18.         />  
  19.     <LinearLayout   
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:orientation="horizontal"  
  23.         android:padding="10dp"  
  24.         >  
  25.         <TextView   
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:id="@+id/tv_author"  
  29.             />  
  30.         <TextView   
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:id="@+id/tv_time"  
  34.             android:layout_marginLeft="20dp"  
  35.             />  
  36.     </LinearLayout>  
  37.       
  38.   
  39. </LinearLayout>  
BaseAdapter适配器

[html]  view plain  copy
  1. package com.bwie.test2.adapter;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import com.bwie.test2.R;  
  6. import com.bwie.test2.bean.Zixun;  
  7.   
  8. import android.support.v4.app.FragmentActivity;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.TextView;  
  13.   
  14. public class MyBaseAdapter extends BaseAdapter {  
  15.   
  16.     private FragmentActivity activity;  
  17.     private ArrayList<Zixun> mZxList;  
  18.   
  19.     public MyBaseAdapter(FragmentActivity activity, ArrayList<Zixun> mZxList) {  
  20.         this.activity=activity;  
  21.         this.mZxList=mZxList;  
  22.     }  
  23.   
  24.     @Override  
  25.     public int getCount() {  
  26.         return mZxList.size();  
  27.     }  
  28.   
  29.     @Override  
  30.     public Object getItem(int arg0) {  
  31.         return null;  
  32.     }  
  33.   
  34.     @Override  
  35.     public long getItemId(int position) {  
  36.         return 0;  
  37.     }  
  38.   
  39.     @Override  
  40.     public View getView(int position, View convertView, ViewGroup parent) {  
  41.         View view = View.inflate(activity, R.layout.fragment1_item, null);  
  42.         TextView tv_title = (TextView) view.findViewById(R.id.tv_title);  
  43.         TextView tv_body = (TextView) view.findViewById(R.id.tv_body);  
  44.         TextView tv_time = (TextView) view.findViewById(R.id.tv_time);  
  45.         TextView tv_author = (TextView) view.findViewById(R.id.tv_author);  
  46.         tv_title.setText(mZxList.get(position).getTitle());  
  47.         tv_body.setText(mZxList.get(position).getBody());  
  48.         tv_time.setText(mZxList.get(position).getPubDate());  
  49.         tv_author.setText(mZxList.get(position).getAuthor());  
  50.         return view;  
  51.     }  
  52.   
  53. }  
MainActivity中设置title与ViewPager关联,滑动ViewPager,title相应的改变颜色,点击按钮,跳到对应的ViewPager的页码

[html]  view plain  copy
  1. package com.bwie.test2;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.bwie.test2.fragment.Fragment1;  
  7. import com.bwie.test2.fragment.Fragment2;  
  8. import com.bwie.test2.fragment.Fragment3;  
  9. import com.bwie.test2.fragment.Fragment4;  
  10.   
  11. import android.os.Bundle;  
  12. import android.app.Activity;  
  13. import android.graphics.Color;  
  14. import android.support.v4.app.Fragment;  
  15. import android.support.v4.app.FragmentActivity;  
  16. import android.support.v4.app.FragmentPagerAdapter;  
  17. import android.support.v4.view.ViewPager;  
  18. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  19. import android.view.View;  
  20. import android.view.View.OnClickListener;  
  21. import android.widget.Button;  
  22. import android.widget.RadioButton;  
  23. import android.widget.RadioGroup;  
  24.   
  25. public class MainActivity extends FragmentActivity implements OnClickListener{  
  26.   
  27.     private RadioGroup group;  
  28.     private RadioButton button;  
  29.     private RadioButton button1;  
  30.     private RadioButton button2;  
  31.     private RadioButton button3;  
  32.     private ViewPager viewPager;  
  33.     private List<Button> list;  
  34.   
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.activity_main);  
  39.         group = (RadioGroup) findViewById(R.id.radioGroup1);  
  40.         button = (RadioButton) findViewById(R.id.radio0);  
  41.         button1 = (RadioButton) findViewById(R.id.radio1);  
  42.         button2 = (RadioButton) findViewById(R.id.radio2);  
  43.         button3 = (RadioButton) findViewById(R.id.radio3);  
  44.         viewPager = (ViewPager) findViewById(R.id.viewPager);  
  45.         list = new ArrayList<Button>();  
  46.         list.add(button);  
  47.         list.add(button1);  
  48.         list.add(button2);  
  49.         list.add(button3);  
  50.         for (Button button : list) {  
  51.             button.setOnClickListener(this);  
  52.         }  
  53.         viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {  
  54.               
  55.             @Override  
  56.             public int getCount() {  
  57.                 return 4;  
  58.             }  
  59.               
  60.             @Override  
  61.             public Fragment getItem(int arg0) {  
  62.                 Fragment fragment = null;  
  63.                 switch (arg0) {  
  64.                 case 0:  
  65.                     fragment=new Fragment1();  
  66.                     break;  
  67.                 case 1:  
  68.                     fragment=new Fragment2();  
  69.                     break;  
  70.                 case 2:  
  71.                     fragment=new Fragment3();  
  72.                     break;  
  73.                 case 3:  
  74.                     fragment=new Fragment4();  
  75.                     break;  
  76.   
  77.                 default:  
  78.                     break;  
  79.                 }  
  80.                 return fragment;  
  81.             }  
  82.         });  
  83.         viewPager.setOnPageChangeListener(new OnPageChangeListener() {  
  84.   
  85.             @Override  
  86.             public void onPageSelected(int position) {  
  87.                 // TODO Auto-generated method stub  
  88.                 for (int i = 0; i < list.size(); i++) {  
  89.                     if (position == i) {  
  90.                         list.get(i).setTextColor(Color.GREEN);  
  91.   
  92.                     } else {  
  93.                         list.get(i).setTextColor(Color.BLACK);  
  94.   
  95.                     }  
  96.                 }  
  97.             }  
  98.   
  99.             @Override  
  100.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  101.                 // TODO Auto-generated method stub  
  102.   
  103.             }  
  104.   
  105.             @Override  
  106.             public void onPageScrollStateChanged(int arg0) {  
  107.                 // TODO Auto-generated method stub  
  108.   
  109.             }  
  110.         });  
  111.     }  
  112.   
  113.     @Override  
  114.     public void onClick(View v) {  
  115.         switch (v.getId()) {  
  116.         case R.id.radio0 :  
  117.             button.setTextColor(Color.GREEN);  
  118.             button1.setTextColor(Color.BLACK);  
  119.             button2.setTextColor(Color.BLACK);  
  120.             button3.setTextColor(Color.BLACK);  
  121.             viewPager.setCurrentItem(0);  
  122.             break;  
  123.         case R.id.radio1 :  
  124.             button.setTextColor(Color.BLACK);  
  125.             button1.setTextColor(Color.GREEN);  
  126.             button2.setTextColor(Color.BLACK);  
  127.             button3.setTextColor(Color.BLACK);  
  128.             viewPager.setCurrentItem(1);  
  129.             break;  
  130.         case R.id.radio2 :  
  131.             button.setTextColor(Color.BLACK);  
  132.             button1.setTextColor(Color.BLACK);  
  133.             button2.setTextColor(Color.GREEN);  
  134.             button3.setTextColor(Color.BLACK);  
  135.             viewPager.setCurrentItem(2);  
  136.             break;  
  137.         case R.id.radio3 :  
  138.             button.setTextColor(Color.BLACK);  
  139.             button1.setTextColor(Color.BLACK);  
  140.             button2.setTextColor(Color.BLACK);  
  141.             button3.setTextColor(Color.GREEN);  
  142.             viewPager.setCurrentItem(3);  
  143.             break;  
  144.   
  145.         default:  
  146.             break;  
  147.         }  
  148.     }  
  149.   
  150. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值