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(); } }
package com.bawei.vo;

public class Good {
private String title;
private String link;
private String imgs;
private String description;
private String copyright;
private String pubDate;
private String comments;
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}
public String getImgs() {
    return imgs;
}
public void setImgs(String imgs) {
    this.imgs = imgs;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getCopyright() {
    return copyright;
}
public void setCopyright(String copyright) {
    this.copyright = copyright;
}
public String getPubDate() {
    return pubDate;
}
public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
}
public String getComments() {
    return comments;
}
public void setComments(String comments) {
    this.comments = comments;
}
@Override
public String toString() {
    return "Good [title=" + title + ", link=" + link + ", imgs=" + imgs
            + ", description=" + description + ", copyright=" + copyright
            + ", pubDate=" + pubDate + ", comments=" + comments + "]";
}
public Good(String title, String link, String imgs, String description,
        String copyright, String pubDate, String comments) {
    super();
    this.title = title;
    this.link = link;
    this.imgs = imgs;
    this.description = description;
    this.copyright = copyright;
    this.pubDate = pubDate;
    this.comments = comments;
}
public Good() {
    super();
}

}

 另一种网络请求

private InputStream loginByServerByPost(String url1) {

        InputStream rs = null;
        URL url = null;
        HttpURLConnection urlConn = null;

        try {
            url = new URL(url1);// 创建和服务器的连接对象URL
            urlConn = (HttpURLConnection) url.openConnection();// 打开连接
            urlConn.setConnectTimeout(5 * 1000);// 设置连接超时容忍时间
            urlConn.setReadTimeout(5 * 1000);// 设置读取时间
            if (urlConn.getResponseCode() == 200) {// 如果响应码为200表示响应成功,并且同时成功的相应了数据
                // 获得服务器相应的数据,字节输入流(数据流),转换为缓存字符流便于读取
                rs = urlConn.getInputStream();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rs;// 转换为字符串,返回

    };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值