好久未用WS,最近迫于课程需要(天气),又拿出了年前写的那些东西,在这里做下整理、反省。
找了好久,找到一个自认为应该是稳定的天气接口(中国气象局的:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx),能够预报未来3天的天气,不知道准不准。
public class MyWeather extends Activity { //这些在WSDL中都可以获得 private static final String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; private static final String METHOD_NAME = "getWeatherbyCityName"; private static final String NAMESPACE = "http://WebXml.com.cn/"; private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView weather=new TextView(this); try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("theCityName", "杭州"); AndroidHttpTransport aht = new AndroidHttpTransport(URL); aht.debug = true; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; envelope.dotNet = true; envelope.setOutputSoapObject(request); aht.call(SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope.bodyIn; SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult"); weather.setText(detail.getProperty(6).toString()+" "+detail.getProperty(5).toString()+" "+detail.getProperty(7).toString()); setContentView(weather); } catch (Exception e) { e.printStackTrace(); } } }