Android 访问 WSDL接口复习
什么是 webserviece
可以到 http://www.w3school.com.cn/webservices/ 去了解一下
重要的内容如下:
Web Services 可以将应用程序转换为网络应用程序。
通过使用 Web Services,您的应用程序可以向全世界发布信息,或提供某项功能。
Web Services 可以被其他应用程序使用。
通过 Web Services,您的会计部门的Win 2k服务器可以与IT供应商的UNIX服务器相连接。
基本的 Web Services 平台是XML+HTTP。
Web services 使用 XML来编解码数据,并使用SOAP来传输数据。
重要的是:基于http post 的传输协议soap;
soap 是什么;
SOAP消息基本上是从发送端到接收端的单向传输,但它们常常结合起来执行类似于请求 /应答的模式。
所有的 SOAP消息都使用XML编码。一条SOAP消息就是一个包含有一个必需的SOAP的封装包,
一个可选的 SOAP 标头和一个必需的SOAP体块的XML文档。
把 SOAP 绑定到HTTP提供了同时利用SOAP的样式和分散的灵活性的特点以及HTTP的丰富的特征库的优点。
在HTTP上传送SOAP并不是说SOAP会覆盖现有的HTTP语义,而是HTTP上的SOAP语义会自然的映射到HTTP语义。
在使用 HTTP 作为协议绑定的场合中,RPC请求映射到HTTP请求上,而RPC应答映射到HTTP应答。
然而,在 RPC 上使用SOAP并不仅限于HTTP协议绑定。SOAP也可以绑定到TCP和UDP协议上。
来看一下我们android 中如何访问一个webservice接口,说白 就是如何从目标wsdl文件 获取信息;
直接上代码;
小白我问题又来了:请求网络用什么方法了,或者用什么类访问一个wsdl 文件了???
来访问一把看看;
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
好吧,废话不说直接说:
Android 端访问 wsdl文件需要一个第三方类库Ksoap2-android
网上关于Ksoap2-android 很多自己有兴趣可以一查;
Ksoap2-android简介
在Android平台调用Web Service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。认真读完对ksoap2的介绍你会发现并没有提及它应用于Android平台开发,没错,在Android平台中我们并不会直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android是Android平台上一个高效、轻量级的SOAP开发包,等同于Android平台上的KSoap2的移植版本。
猜猜我下好了这个jar
贴出访问的核心代码;一个外网的。.net 的 wsdl
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getSupportCity xmlns="http://WebXml.com.cn/">
<byProvinceName>string</byProvinceName>
</getSupportCity>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getSupportCityResponse xmlns="http://WebXml.com.cn/">
<getSupportCityResult>
<string>string</string>
<string>string</string>
</getSupportCityResult>
</getSupportCityResponse>
</soap12:Body>
</soap12:Envelope>
package com.xiangsc.webserversdemo;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivityextends Activityimplements OnClickListener {
private EditText nr;
private Button get;
private TextView tv;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==200){
String showinfo=(String)msg.obj;
tv.setText(showinfo);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void loadInfo(final String snm) {
new Thread() {
public void run() {
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(Const.WeBNamespace,Const.Method);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("byProvinceName", snm);
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService 此句话看你调用后台是不是.net 不是就改为false
// 我在项目中用的是JSF gai wei false
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
//HttpTransportSE so 网络请求的封装类
HttpTransportSE transport = new HttpTransportSE(Const.URL);
try {
// 调用WebService
transport.call(Const.WeBNamespace + Const.Method, envelope);// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
Message msg=Message.obtain();
msg.what=200;
msg.obj=result;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private void initView() {
nr = (EditText) findViewById(R.id.et);
get = (Button) findViewById(R.id.get);
get.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
String snm=nr.getText().toString().trim();
if(!TextUtils.isEmpty(snm)){
loadInfo(snm);
}
}
}
运行结果如图,Demo 非常陋大神们莫笑
Demo下载地址 http://download.****.net/detail/qq_30519365/9533911