HttpUtils:

package cn.qf.parser;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
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.util.EntityUtils; /**
* HttpClient请求网络 获取XML数据
*
* @author my
*
*/
public class HttpUtils {
/**
* Http请求网络获取xml数据
* 下载图片
* @param path
*/
public static void downLoadImg(String path) {
BufferedOutputStream bos = null;
try {
// 1.创建HttpCLient对象
HttpClient httpClient = new DefaultHttpClient();
// 2.创建请求对象 指定地址
HttpGet htpGet = new HttpGet(path);
// 3.执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(htpGet);
// 4.获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 获得响应的HttpEntity对象
HttpEntity entity = response.getEntity();
String name = path.substring(path.lastIndexOf("/") + 1);
bos = new BufferedOutputStream(new FileOutputStream(name));
bos.write(EntityUtils.toByteArray(entity));
bos.flush();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 从网络下载Xml文件
* @param path
* @return
*/
public static String getXMLByInternet(String path) {
try {
// 1.创建httpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 2.创建请求对象 指定地址
HttpGet httpGet = new HttpGet(path);
// 3.执行请求,获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4.获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 获得响应的httpEntity对象__客户端服务端传递的载体
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "GBK");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
}

ParserDemo.java

package com.qf.PullParser4;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; /**
* http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=
* mXBIDrvTOwwmYaTtN03Lo0j2 请求上述接口,使用pull解析<date>字段和<dayPictureUrl>字段,并把
* <dayPictureUrl>字段对应的图片下载到本地, 展示到jsp页面
*
* @author my
*
*/
class WeatherDate {
private String date;
private String dayPictureUrl; public String getdate() {
return date;
} public void setdate(String date) {
this.date = date;
} public String getDayPictureUrl() {
return dayPictureUrl;
} public void setDayPictureUrl(String dayPictureUrl) {
this.dayPictureUrl = dayPictureUrl;
} @Override
public String toString() {
return "WeatherDate [date=" + date + ", dayPictureUrl=" + dayPictureUrl + "]";
} } public class parserDemo {
public static void main(String[] args) {
String xml = HttpUtils.getXMLByInternet(
"http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2");
System.out.println(xml);
try {
// 创建解析器工厂对象
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 创建解析器
XmlPullParser parser = factory.newPullParser();
// 设置数据源,
       //StringReader 是Reader的子类
       // StringReader 的数据源是String类型的字符串
         parser.setInput(new StringReader(xml));
// 得到响应码
int code = parser.getEventType();
WeatherDate weatherDate = null;
List<WeatherDate> list = null;
String temp = "";
while (code != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
switch (code) {
case XmlPullParser.START_TAG:
if ("weather_data".equals(name)) {
list = new ArrayList<>();
temp = "weather";
} else if ("date".equals(name) && "weather".equals(temp)) {
weatherDate = new WeatherDate();
weatherDate.setdate(parser.nextText());
} else if ("dayPictureUrl".equals(name)) {
String path = parser.nextText();
weatherDate.setDayPictureUrl(path);
list.add(weatherDate);
// 下载图片
HttpUtils.downLoadImg(path);
}
break;
default:
break;
}
code = parser.next();
}
for (WeatherDate weatherDate2 : list) {
System.out.println(weatherDate2);
} } catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}

最新文章

  1. VS2015插件
  2. 简单的jQuery幻灯片实现
  3. [OC] 图片平铺
  4. ruby on rails 安装
  5. 学习使用 SVG 创建材料设计涟漪(Ripple)效果
  6. lua 面向对象
  7. editorial-render A
  8. 获取Assets目录下的图片显示并上传
  9. factory工厂模式之工厂方法FactoryMethod
  10. 使用Cygwin通过ssh命令行来访问Windows8
  11. 小白日记6:kali渗透测试之被动信息收集(五)-Recon-ng
  12. Centos 6.5 配置nginx服务
  13. 转:常用的HTML标签和属性解释
  14. (转载)IQ 16.0 SP02起支持从压缩文件直接装载数据到表中
  15. BZOJ 4455: [Zjoi2016]小星星 [容斥原理 树形DP]
  16. python之路(11)描述符
  17. 部署自建CA颁发证书实现https加密
  18. ubuntu18.04LTS设置静态IP
  19. 老男孩python学习自修第四天【字典的使用】
  20. HNU 2015暑期新队员训练赛2 B Combination

热门文章

  1. Spring_跨项目读取properties文件
  2. caffe-mnist别手写数字
  3. 元组tuple
  4. Windbg对过滤驱动DriverEntry函数下断点技巧
  5. 业务对象和BAPI
  6. 虚拟机guest为windows7的环境下安装破解版simplify3d_3.0.2
  7. 实现简单的WebPart
  8. 内容提供者(Content Provider)——跨程序共享数据
  9. android 解决ViewPager双层嵌套的滑动问题
  10. js轮盘抽奖
  11. iOS长按选择
  12. WebApi2 文件图片上传下载
  13. 关于cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题(不需要修改任何代码,只需要一个配置)
  14. 利用wsdl.exe自动将wsdl文档转换为C#代码
  15. Thymleaf js直接获取后台传过来的对象或者对象的属性以及map
  16. Linux 添加到环境变量
  17. cvte春招测试面试记录
  18. awk入门【转】
  19. MSF MS17_010漏洞测试
  20. python中的下划线