ITPub博客

首页 > 应用开发 > IT综合 > android处理webservice

android处理webservice

原创 IT综合 作者:livedba 时间:2011-04-13 13:10:00 0 删除 编辑
这个类接收城市名称,返回天气字符串[@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 = "http://WebXml.com.cn/"; //WebService地址
//private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeather";

//private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
private static String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";

//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;//系统维护 免费用户服务暂停。http://www.webxml.com.cn/;
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博客 ” ,链接:https://blog.itpub.net/25261409/viewspace-1048604/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: Android NDK
请登录后发表评论 登录
全部评论

注册时间:2011-01-21

  • 博文量
    115
  • 访问量
    483881