完整源代码:获取天气预报(天气,气温,风力...)WebService - 云代码
部分核心代码:
public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("theCityName", cityName);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
// ht.call(null, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result
.getProperty("getWeatherbyCityNameResult");
System.out.println("result" + result);
System.out.println("detail" + detail);
Toast.makeText(MainActivity.this, detail.toString(),
Toast.LENGTH_LONG).show();
parseWeather(detail);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split(" ")[0];
weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
weatherToday = weatherToday + "\n气温:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday + "\n风力:"
+ detail.getProperty(7).toString() + "\n";
System.out.println("weatherToday is " + weatherToday);
Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)
.show();
}