这个类接收城市名称,返回天气字符串[@more@]
package com.demo.weather;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
public class WeatherReport implements IWeather{
private static String LOG_TAG = "Weather";
private static boolean DEBUG = false;
private static final int SHOW_ABOUT = 0x0001;
private static final String NAMESPACE = ""; //WebService地址
//private static String SOAP_ACTION = "";
private static String SOAP_ACTION = "";
//private static String URL = "";
private static String URL = "";
//private static final String METHOD_NAME = "getWeatherbyCityName";
private static final String METHOD_NAME = "getWeather";
private SoapObject detail = null;
public WeatherBean getWeatherBean(String paramString){
WeatherBean wb = new WeatherBean();
detail = getWeatherSoap(paramString);
if(detail == null) return null;
if(detail.getPropertyCount()<5) return null;//系统维护 免费用户服务暂停。;
String date = detail.getProperty(7).toString();
StringBuffer weatherToday = new StringBuffer();
weatherToday.append(date.split(" ")[0]);
weatherToday.append("n天气:");
weatherToday.append(date.split(" ")[1]);
weatherToday.append("n气温:");
weatherToday.append(detail.getProperty(8).toString());
weatherToday.append("n风力:");
weatherToday.append(detail.getProperty(9).toString());
weatherToday.append("n");
wb.setWeatherToday(weatherToday.toString());
wb.setWeatherCurrent(detail.getProperty(4).toString());
StringBuffer weatherTomorrow = new StringBuffer();
date = detail.getProperty(12).toString();
weatherTomorrow.append(date.split(" ")[0]);
weatherTomorrow.append("n天气:");
weatherTomorrow.append(date.split(" ")[1]);
weatherTomorrow.append("n气温:");
weatherTomorrow.append(detail.getProperty(13).toString());
weatherTomorrow.append("n风力:");
weatherTomorrow.append(detail.getProperty(14).toString());
weatherTomorrow.append("n");
wb.setWeatherTomorrow(weatherTomorrow.toString());
StringBuffer weatherAfterday = new StringBuffer();
date = detail.getProperty(17).toString();
weatherAfterday.append(date.split(" ")[0]);
weatherAfterday.append("n天气:");
weatherAfterday.append(date.split(" ")[1]);
weatherAfterday.append("n气温:");
weatherAfterday.append(detail.getProperty(18).toString());
weatherAfterday.append("n风力:");
weatherAfterday.append(detail.getProperty(19).toString());
weatherAfterday.append("n");
wb.setWeatherAfterday(weatherAfterday.toString());
StringBuffer weatherSuggest = new StringBuffer();
weatherSuggest.append(detail.getProperty(6).toString());
wb.setWeatherSuggest(weatherSuggest.toString());
return wb;
}
public SoapObject getWeatherSoap(String paramString){
try{
//第一步:创建SoapObject对象,指定WebService的命名空间和调用方法
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
//rpc.addProperty("theCityName",paramString);
rpc.addProperty("theCityCode",paramString);
//创建SoapSerializationEnvelope对象,并指定WebService的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//设置bodyOut属性
envelope.bodyOut=rpc;
//设置.net的WebService,不然出错
envelope.dotNet=true;
envelope.setOutputSoapObject(rpc);
//创建HttpTransport对象,指定WSDL文档的URL
AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
//调用WebService
ht.call(SOAP_ACTION,envelope);
//SoapObject result = (SoapObject)envelope.bodyIn;
//detail = (SoapObject)result.getProperty("getWeatherbyCityNameResult");
//detail = (SoapObject)result.getProperty("getWeatherResponse");
SoapObject result = (SoapObject)envelope.getResponse();
return result;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
package com.demo.weather;
import java.io.Serializable;
public class WeatherBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//今天天气
private String weatherToday;
private String weatherTomorrow;
private String weatherAfterday;
private String weatherCurrent;
private String weatherSuggest;
public String getWeatherSuggest() {
return weatherSuggest;
}
public void setWeatherSuggest(String weatherSuggest) {
this.weatherSuggest = weatherSuggest;
}
public String getWeatherToday() {
return weatherToday;
}
public void setWeatherToday(String weatherToday) {
this.weatherToday = weatherToday;
}
public String getWeatherTomorrow() {
return weatherTomorrow;
}
public void setWeatherTomorrow(String weatherTomorrow) {
this.weatherTomorrow = weatherTomorrow;
}
public String getWeatherAfterday() {
return weatherAfterday;
}
public void setWeatherAfterday(String weatherAfterday) {
this.weatherAfterday = weatherAfterday;
}
public String getWeatherCurrent() {
return weatherCurrent;
}
public void setWeatherCurrent(String weatherCurrent) {
this.weatherCurrent = weatherCurrent;
}
public String toString(){
return "欢迎使用天气预报组件";
}
}
package com.demo.weather;
public interface IWeather {
public abstract WeatherBean getWeatherBean(String paramString);
}