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;// 转换为字符串,返回 };

HttpEntity转换Inputstream(红色)加XmlPull解析的更多相关文章

  1. Away3D引擎学习笔记(一)资源加载解析块

    前文:Away3D断断续续用了一段时间了,三维相关的很多算法,计算转换还是有点绕,整理些自己觉得还有点意思东西,希望大家有用. 三维开始,Away3D构架你场景那几行代码各处都有,这里就不copy了, ...

  2. Hibernate懒加载解析

    Hibernate懒加载解析 在Hibernate框架中,当我们要访问的数据量过大时,明显用缓存不太合适, 因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,这时Hibernate用懒加载机制来 ...

  3. JAVA时间进行比较和转换,时间加减得到天数

    转自:https://blog.csdn.net/iteye_8535/article/details/82246006 JAVA时间进行比较和转换,时间加减得到天数 1. 把时间类型的字符串转为DA ...

  4. <script>标签的加载解析执行

    转自原文 <script>标签的加载解析执行 看了很多网上的文章,都是大同小异.总结一下.内部原理还没有搞清楚,有机会再学习. 一.<script>标签的加载解析执行顺序 ht ...

  5. poi解析word文档转换成html(包括图片解析)

    需求:将本地上传的word文档解析并放入数据库中 代码: import java.io.ByteArrayOutputStream;import java.io.File;import java.io ...

  6. js 每日一更(数组转换成前端更容易解析的树状结构)

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  7. XMLPuLL解析

    1 package com.bawei.day14_xmlpull; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 i ...

  8. Android之使用XMLPull解析xml(二)

    转自:http://www.blogjava.net/sxyx2008/archive/2010/08/04/327885.html 介绍下在Android中极力推荐的xmlpull方式解析xml.x ...

  9. Android--使用XMLPull解析xml

    在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...

随机推荐

  1. Git系列教程三 配置与基本命令

    一.安装Git 网上有很多安装教程,可以参考.这里使用的是Windows版本的Git,点击这里下载. 二.基本设置 安装完成后,通过点击鼠标右键就可以看到新添加了俩个Git命令:Git GUI Her ...

  2. [译] jQuery 3 有哪些新东西

    转自:https://github.com/cssmagic/blog/issues/59 jQuery 的横空出世,至今已有十个年头了,而它的长盛不衰显然不是没有理由的.jQuery 提供了极为友好 ...

  3. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  4. IntelliJ Idea 常用快捷键列表

    Alt+回车 导入包,自动修正 sout+Tab Syso快捷键 Ctrl+Alt+F 局部变量变为全局变量 Ctrl+Alt+V 类似于eclipse中Ctrl+2+L的快捷键 Ctrl+Alt+T ...

  5. URL转Drawable之 Android中获取网络图片的三种方法

    转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...

  6. css background-size 属性 兼容ie8 ie7 方案

    <!-- background-size-polyfill v0.2.0 | (c) 2012-2013 Louis-Rémi Babé | MIT License --> <PUB ...

  7. zTree的内核

    zTree的内核:   *  结构       (function($){           //常量部分           //申明常量是因为这些数据是不能改变的,如果成了对象的属性,很容易就改 ...

  8. 【Android测试】Android截图的深水区

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6113059.html 需求 这两天遇到这样一个事情,因为某 ...

  9. canvas简单图片处理(灰色处理)

    反色处理写的比较简单,灰色处理写了一些注释 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  10. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...