完整源代码:获取天气预报(天气,气温,风力...)WebService - 云代码

 

核心代码:

 
  
  1. public void getWeather(String cityName) {  
  2.     try {  
  3.         SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
  4.         rpc.addProperty("theCityName", cityName);  
  5.  
  6.         HttpTransportSE ht = new HttpTransportSE(URL);  
  7.         ht.debug = true;  
  8.  
  9.         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(  
  10.                 SoapEnvelope.VER11);  
  11.  
  12.         envelope.bodyOut = rpc;  
  13.         envelope.dotNet = true;  
  14.         envelope.setOutputSoapObject(rpc);  
  15.  
  16.         ht.call(SOAP_ACTION, envelope);  
  17.         // ht.call(null, envelope);  
  18.  
  19.         SoapObject result = (SoapObject) envelope.bodyIn;  
  20.         detail = (SoapObject) result  
  21.                 .getProperty("getWeatherbyCityNameResult");  
  22.  
  23.         System.out.println("result" + result);  
  24.         System.out.println("detail" + detail);  
  25.         Toast.makeText(MainActivity.this, detail.toString(),  
  26.                 Toast.LENGTH_LONG).show();  
  27.         parseWeather(detail);  
  28.  
  29.         return;  
  30.     } catch (Exception e) {  
  31.         e.printStackTrace();  
  32.     }  
  33. }  
  34.  
  35. private void parseWeather(SoapObject detail)  
  36.         throws UnsupportedEncodingException {  
  37.     String date = detail.getProperty(6).toString();  
  38.     weatherToday = "今天:" + date.split(" ")[0];  
  39.     weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];  
  40.     weatherToday = weatherToday + "\n气温:" 
  41.             + detail.getProperty(5).toString();  
  42.     weatherToday = weatherToday + "\n风力:" 
  43.             + detail.getProperty(7).toString() + "\n";  
  44.     System.out.println("weatherToday is " + weatherToday);  
  45.     Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)  
  46.             .show();  
  47.  
  48. }