HttpEntity转换Inputstream(红色)加XmlPull解析

package com.bawei.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.bawei.vo.Good;

import android.os.Bundle;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;

public class MainActivity extends Activity {
    private String URL = "http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20&pass=";
    private InputStream st;
    private List<Good> list;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                HttpGet get = new HttpGet(URL);
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
                HttpConnectionParams.setSoTimeout(params, 5 * 1100);
                HttpClient client = new DefaultHttpClient(params);
                try {
                    HttpResponse res = client.execute(get);
                    if (res.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = res.getEntity();
                        st = entity.getContent();
                        System.out.println(st);
                        

list
= new ArrayList<Good>(); XmlPullParser parser = Xml.newPullParser(); try { try { parser.setInput(st, "utf-8"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } int eventType = parser.getEventType(); String tagName = ""; Good good =null; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: tagName = parser.getName(); if("item".equals(tagName)){ good = new Good(); } break; case XmlPullParser.TEXT: String text = parser.getText(); if("title".equals(tagName)){ good.setTitle(text); }else if("link".equals(tagName)){ good.setLink(text); }else if("imgs".equals(tagName)){ good.setImgs(text); }else if("description".equals(tagName)){ good.setDescription(text); }else if("copyright".equals(tagName)){ good.setCopyright(text); }else if("pubDate".equals(tagName)){ good.setPubDate(text); }else if("comments".equals(tagName)){ good.setComments(text); }else break; case XmlPullParser.END_TAG: tagName= parser.getName(); if("item".equals(tagName)){ list.add(good); System.out.println(list.toString()); good=null; } tagName=""; break; }
//继续 eventType
=parser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }



} }
catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } }

你可能感兴趣的:(HttpEntity转换Inputstream(红色)加XmlPull解析)