三、新闻和天气是通过webservice来实现
1,天气是请求公共的webservice,新闻自己搭了webservices
关于webservices的请求部分
public static List<String> getWeatherList(String tcity) throws Exception { String path = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; InputStream inStream = WeatherService.class.getClassLoader() .getResourceAsStream("soap.xml"); String soap =utils.readFile(inStream); soap = soap.replaceAll("\\$thecity", tcity); byte[] data = soap.getBytes();
HttpURLConnection conn = (HttpURLConnection) new URL(path) .openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); conn.getOutputStream().write(data); //Log.d("myout", String.valueOf(conn.getResponseCode())); if (conn.getResponseCode() == 200) { return utils.parseXML(conn.getInputStream()); } return null; }
parseXML完成节点的分析
/** * 解析返回的SOAP协议 * * @param inputStream * @return */ public static List<String> parseXML(InputStream inputStream) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(inputStream, "UTF-8");
List<String> list=new ArrayList<String>(); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start document:"); } else if (eventType == XmlPullParser.END_DOCUMENT) { System.out.println("End document"); } else if (eventType == XmlPullParser.START_TAG) { //System.out.println(" " + parser.getName());
} else if (eventType == XmlPullParser.END_TAG) { //System.out.println("222 " + parser.getName()); } else if (eventType == XmlPullParser.TEXT) { list.add(parser.getText()); //System.out.println(" 111" + parser.getText()); } eventType = parser.next();
}
return list;
}
soap请求的格式,在webserivices服务里有说明
<?xml version="1.0" encoding="UTF-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> <theCityName>$thecity</theCityName> </getWeatherbyCityName> </soap12:Body> </soap12:Envelope>
2,新闻部分大同小异了,通过一个listactivity来显示新闻列表,通过一个带webkit的activity来来显示新闻详细的页面
详细看代码吧
源码在群共享里:72349065,欢迎来一起交流学习
转载于:https://www.cnblogs.com/pxue/archive/2011/06/27/2091755.html
相关资源:数据结构—成绩单生成器