标签:
WebService
从牛腩新闻系统后来了解到WebService,这是一种基于SOAP协议的远程调用标准,通
过webservice可以将不同操作系统平台、不同语言、不同技术整合到一块。在Android
SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用
WebService。PC版本的WEbservice客户端库非常丰富,例如Axis2,CXF等,但这些开发
包对于Android系统过于庞大,也未必很容易移植到Android系统中。因此,这些开发包
并不是在我们的考虑范围内。适合手机的WebService客户端的SDK有一些,比较常用的有
Ksoap2,可以从http://code.google.com/p/ksoap2-android/downloads/list进行下
载;将下载的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包复制到
Eclipse工程的lib目录中,当然也可以放在其他的目录里。同时在Eclipse工程中引用这
个jar包。
SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到
WebService的命名空间。
第二个参数表示要调用的WebService方法名。
(2)设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如
下:
<span style="font-family:KaiTi_GB2312;font-size:24px;">Request.addProperty(“param1”,”value”); Request.addProperty(“param2”,”value”);</span>
不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
(3)生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope
对象描述,代码为:
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); Envelope.bodyOut = request;
造方法设置SOAP协议的版本号。该版本号需要根据服务端WebService的版本号设置。在
创建SoapSerializationEnvelope对象后,不要忘了设置
SOAPSoapSerializationEnvelope类的bodyOut属性,该属性的值就是在第一步创建的
SoapObject对象。
(4)创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL:HttpTransportSE ht=new
HttpTransportSE(“http://192.168.18.17:80
/axis2/service/SearchNewsService?wsdl”);
(5)使用call方法调用WebService方法,代码:
ht.call(null,envelope);
Call方法的第一个参数一般为null,第2个参数就是在第3步创建的
SoapSerializationEnvelope对象。
(6)使用getResponse方法获得WebService方法的返回结果,代码:
SoapObject soapObject =( SoapObject) envelope.getResponse();
以下为简单的实现一个查看当前天气功能的例子:
<span style="font-family:KaiTi_GB2312;font-size:24px;">publicclass WebService extends Activity { privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/"; // WebService地址 privatestatic String URL ="http://www.webxml.com.cn/ webservices/weatherwebservice.asmx"; privatestaticfinal String METHOD_NAME ="getWeatherbyCityName"; privatestatic String SOAP_ACTION ="http://WebXml.com.cn/ getWeatherbyCityName"; private String weatherToday; private Button okButton; private SoapObject detail; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); okButton = (Button) findViewById(R.id.ok); okButton.setOnClickListener(new Button.OnClickListener() { publicvoid onClick(View v) { showWeather(); } }); } privatevoid showWeather() { String city ="北京"; getWeather(city); } @SuppressWarnings("deprecation") publicvoid getWeather(String cityName) { try { System.out.println("rpc------"); SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME); System.out.println("rpc"+ rpc); System.out.println("cityName is "+ cityName); rpc.addProperty("theCityName", cityName); AndroidHttpTransport ht =new AndroidHttpTransport(URL); //AndroidHttpTransport 是一个要过期的类,可以使用HttpTransportsSE对象 ht.debug =true; SoapSerializationEnvelope envelope =new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet =true; envelope.setOutputSoapObject(rpc); ht.call(SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope.bodyIn; detail = (SoapObject) result .getProperty("getWeatherbyCityNameResult"); System.out.println("result"+ result); System.out.println("detail"+ detail); Toast.makeText(WebService.this, detail.toString(), Toast.LENGTH_LONG).show(); parseWeather(detail); return; } catch (Exception e) { e.printStackTrace(); } } privatevoid 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(WebService.this, weatherToday, Toast.LENGTH_LONG).show(); } } </span>
当手机网络为wap的情况下,就连接失败。解决办法:ksoap2-android最新版本是
2.5.4。在2.5.2之前源码都是不支持代理访问的,在2.5.4之后,源码增加了对网络有代
理的支持。
<span style="font-family:KaiTi_GB2312;font-size:24px;">//判断当前网络是否是net,true为net,false为wap public static boolean isGprsNet(){ String proxyHost = android.net.Proxy.getDefaultHost(); return proxyHost==null; } //根据网络类型返回相应的HttpTransportSE webservice用 public static HttpTransportSE getHttpTransportSE(Context context){ HttpTransportSE ht; if(!HttpUtil.isGprsNet()){ java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort())); ht=new HttpTransportSE(p,context.getString(R.string.wsurl)); }else{ ht = new HttpTransportSE(context.getString(R.string.wsurl)); } return ht; } /* * 登录接口 1成功 0失败 */ public String login(Context context,String phoneNumber,String pwd){ SoapObject request = new SoapObject(context.getString(R.string.namespace), "login"); request.addProperty("usrPhone", phoneNumber); request.addProperty("passWord", pwd); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 ); envelope.bodyOut=request; HttpTransportSE ht = HttpUtil.getHttpTransportSE(context); try { ht.call(null, envelope); } catch (Exception e) {} SoapObject so = null; so = (SoapObject) envelope.bodyIn; String result= so.getProperty("return").toString(); return result; } </span>
androi下net和wap自适应代码:
<span style="font-family:KaiTi_GB2312;font-size:24px;">private HttpURLConnection getURLConnection(String url) throws Exception { String proxyHost = android.net.Proxy.getDefaultHost(); if (proxyHost != null) { java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort())); return (HttpURLConnection) new URL(url).openConnection(p); } else { return (HttpURLConnection) new URL(url).openConnection(); } } </span>
标签:
原文地址:http://blog.csdn.net/u013067756/article/details/46495169