android處理webservice

livedba發表於2011-04-13
這個類接收城市名稱,返回天氣字串[@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);
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25261409/viewspace-1048604/,如需轉載,請註明出處,否則將追究法律責任。

相關文章