Android利用ksoap2写天气预报应用
2010-06-02 00:54:00 来源:WEB开发网怎么说呢,天气预报的应用可以说是一抓一大把,不过找一个自己心仪的却并不是很容易。这个程序也是用来练手的,看看到底怎么用ksoap写一个适合自己的应用。ksoap怎么说呢,就是一个封装好的soap发送接收解析库,这也只怪Android没有自带这些。
闲话不多说,上代码(本来是全部贴出来的,后来发现博客毕竟是用来写重点的,更多内容还是访问下面那个给出的源码地址吧)。
public class Weather extends Activity {
private static final String NAMESPACE = "http://WebXml.com.cn/";
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("theCityName", cityName);
AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
debug(LOG_TAG, "DUMP>> " + ht.requestDump);
debug(LOG_TAG, "DUMP<< " + ht.responseDump);
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
parseWeather(detail);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseWeather(SoapObject detail) {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split(" ")[0];
weatherToday = weatherToday + " 天气:" + date.split(" ")[1];
更多精彩
赞助商链接