pull解析+httpurlconnection

package com.bwie.activity;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import org.xmlpull.v1.XmlPullParser;


import com.bwie.adapter.MyLvAdapter;
import com.bwie.bean.Bean;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class MainActivity extends Activity {


private ListView lv;
    String path="http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20&pass=";
private InputStream inputStream;
private List listb;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
                listb=(List) msg.obj;
lv.setAdapter(new MyLvAdapter(MainActivity.this,listb));

lv.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Intent intent=new Intent(MainActivity.this,WebViewActivity.class);
intent.putExtra("link", listb.get(position).getLink());
startActivity(intent);
}
});
};
};
private Bean bean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv = (ListView) findViewById(R.id.lv);

initEvent();
}


private void initEvent() {
       new Thread(){
      public void run() {
      //获取网络数据
      getXml();
      pullXml();
      };
       }.start();
}


protected ArrayList pullXml() {
listb = new ArrayList();
XmlPullParser pullParser = Xml.newPullParser();
try {
pullParser.setInput(inputStream, "utf-8");
//设置要解析的xml文件到解析器里
String tagName=null;
//得到读取到的事件类型
int eventType = pullParser.getEventType();
//当文档读取结束,循环结束
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
System.out.println("文档开始读取");
break;
//得到开始标签
case XmlPullParser.START_TAG:
//得到当前开始标签的名字
tagName=pullParser.getName();
System.out.println("文档读取到开始标签:"+tagName);
if("item".equals(tagName)){
bean = new Bean();
}
break;
//读取到文本
case XmlPullParser.TEXT:
if("title".equals(tagName)){
// 获得name标签对应的文本,先获得标签对应的文本,再把文本内容封装起来
// 获得标签对应的文本,pullParser.getText()
bean.setTitle(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("link".equals(tagName)){
bean.setLink(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("imgs".equals(tagName)){
bean.setImgs(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("description".equals(tagName)){
bean.setDescription(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("copyright".equals(tagName)){
bean.setCopyright(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("pubDate".equals(tagName)){
bean.setPubDate(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}else if("comments".equals(tagName)){
bean.setComments(pullParser.getText().trim());
System.out.println("文本内容:"+pullParser.getText());
}
break;
//读取到结束标签
case XmlPullParser.END_TAG:
tagName=pullParser.getName();
System.out.println("文档读取到结束标签:"+tagName);
if("item".equals(tagName)){
listb.add(bean);
}
tagName="";
//文档结束读取
case XmlPullParser.END_DOCUMENT:
System.out.println("文档结束读取");
break;

}
//到下个节点 返回下个节点类型
eventType=pullParser.next();
}
//用handler发送一个集合
Message msg=Message.obtain();
msg.obj=listb;
handler.sendMessage(msg);

} catch (Exception e) {
e.printStackTrace();

return (ArrayList) listb;

}


protected void getXml() {
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();

connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
connection.connect();

if(connection.getResponseCode()==200){
inputStream = connection.getInputStream();
}else{
System.out.println("请求失败");
}
} catch (Exception e) {
e.printStackTrace();
}

}



}

你可能感兴趣的:(pull解析+httpurlconnection)