安卓基础干货(四):安卓网络编程的学习 (2)

(1)activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" /> </LinearLayout>

(2)item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <com.loopj.android.image.SmartImageView android:layout_width="80dip" android:layout_height="80dip" android:src="@drawable/ic_launcher" android:id="@+id/iv_image" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_title" android:singleLine="true" android:text="标题标题标题标题标题标题标题标题标题" android:layout_toRightOf="@id/iv_image" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_desc" android:maxLines="3" android:text="描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述" android:layout_toRightOf="@id/iv_image" android:layout_below="@id/tv_title" android:textSize="12sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_type" android:layout_alignParentRight="true" android:text="跟帖" android:layout_below="@id/tv_desc" android:textSize="10sp" /> </RelativeLayout>

2、在子线程中访问网络,获得xml数据

public class MainActivity extends Activity { private ListView lv; private List<NewsItem> list = new ArrayList<NewsItem>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); //从网络上读取数据、解析xml文件 readData(); //使用数据适配器为listview填充数据 lv.setAdapter(new MyAdapter()); } //从网络上读取数据、解析xml文件 private void readData(){ //在子线程中访问网络,获得xml数据 new Thread(){ public void run() { try { String path = "http://192.168.22.136:8080/news.xml"; // 1、发送请求 // (1)创建一个URL对象 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // (2)设置请求头信息 conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); // 2、服务器返回数据 // (1)判断状态码:200 ok,404 没有找到资源、503、509 服务器端错误 int code = conn.getResponseCode(); if(code == 200){ // (2)解析服务器返回的二进制数据,解析成一个字符串 InputStream is = conn.getInputStream(); list = NewsParseService.parseNewsItems(is); }else{ Toast.makeText(MainActivity.this, "请输入一个HTML页面的网络地址", 0).show(); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); }

3、解析xml格式的数据,把数据封装到list

public class NewsParseService { private static List<NewsItem> list; /** * 解析新闻的xml数据返回list * @param is * @return */ public static List<NewsItem> parseNewsItems(InputStream is){ try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "UTF-8"); //得到解析的时间类型 int type = parser.getEventType(); list = new ArrayList<NewsItem>(); NewsItem item = null; while(type != XmlPullParser.END_DOCUMENT){ switch (type) { case XmlPullParser.START_TAG: if("item".equals(parser.getName())){ item = new NewsItem(); }else if("title".equals(parser.getName())){ String title = parser.nextText(); item.setTitle(title); }else if("description".equals(parser.getName())){ String description = parser.nextText(); item.setDescription(description); }else if("image".equals(parser.getName())){ String image = parser.nextText(); item.setImage(image); }else if("type".equals(parser.getName())){ String newsType = parser.nextText(); item.setType(newsType); }else if("comment".equals(parser.getName())){ String comment = parser.nextText(); item.setComment(comment); } break; case XmlPullParser.END_TAG: if("item".equals(parser.getName())){ list.add(item); item = null; } } type = parser.next(); } } catch (Exception e) { e.printStackTrace(); } System.out.println(list.size()+"。。。。。。。"); return list; }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjdds.html