(1)使用ksoap2调用WCF
去ksoap2官网 http://code.google.com/p/ksoap2-android/ 下载最新jar
(2)所使用的webservice(这里只使用第一个接口)
Web服务访问地址: http://t.vip315.net:8800/ScauCsService.svc
现在只提供以下两个接口:
/// <summary>
/// 获取密码,必须传递正确的学号和密码(MD5值过去,才能得到4位纯数字返回值.注意,返回值在1分钟内有效,超过1分钟将会重新生成4位数
/// </summary>
/// <param name="strStudentNumber"></param>
/// <param name="strPassWordMD5"></param>
/// <returns></returns>
string GetMyPassword(string strStudentNumber, string strPassWordMD5);
/// <summary>
/// 获取学号的UPCA条形码,以二进制数组方式返回(返回的是BMP文件的二进制流,内容可直接写入一个文件中,然后作为BMP文件打开)
/// </summary>
/// <param name="strStudentNumber"></param>
/// <param name="strPassWordMD5"></param>
/// <returns></returns>
byte[] GetUPCA(string strStudentNumber, string strPassWordMD5);
(3)根据Web服务访问地址,我们可以找到这个服务的WSDL地址是http://t.vip315.net:8800/ScauCsService.svc?wsdl 。在浏览器上访问我们可以看到以下文档:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="ScauCSService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://t.vip315.net:8800/ScauCsService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://t.vip315.net:8800/ScauCsService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IScauCSService_GetMyPassword_InputMessage">
<wsdl:part name="parameters" element="tns:GetMyPassword"/>
</wsdl:message>
<wsdl:message name="IScauCSService_GetMyPassword_OutputMessage">
<wsdl:part name="parameters" element="tns:GetMyPasswordResponse"/>
</wsdl:message>
<wsdl:message name="IScauCSService_GetUPCA_InputMessage">
<wsdl:part name="parameters" element="tns:GetUPCA"/>
</wsdl:message>
<wsdl:message name="IScauCSService_GetUPCA_OutputMessage">
<wsdl:part name="parameters" element="tns:GetUPCAResponse"/>
</wsdl:message>
<wsdl:message name="IScauCSService_CheckStudentPassword_InputMessage">
<wsdl:part name="parameters" element="tns:CheckStudentPassword"/>
</wsdl:message>
<wsdl:message name="IScauCSService_CheckStudentPassword_OutputMessage">
<wsdl:part name="parameters" element="tns:CheckStudentPasswordResponse"/>
</wsdl:message>
<wsdl:portType name="IScauCSService">
<wsdl:operation name="GetMyPassword">
<wsdl:input wsaw:Action="http://tempuri.org/IScauCSService/GetMyPassword" message="tns:IScauCSService_GetMyPassword_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IScauCSService/GetMyPasswordResponse" message="tns:IScauCSService_GetMyPassword_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetUPCA">
<wsdl:input wsaw:Action="http://tempuri.org/IScauCSService/GetUPCA" message="tns:IScauCSService_GetUPCA_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IScauCSService/GetUPCAResponse" message="tns:IScauCSService_GetUPCA_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="CheckStudentPassword">
<wsdl:input wsaw:Action="http://tempuri.org/IScauCSService/CheckStudentPassword" message="tns:IScauCSService_CheckStudentPassword_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IScauCSService/CheckStudentPasswordResponse" message="tns:IScauCSService_CheckStudentPassword_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IScauCSService" type="tns:IScauCSService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetMyPassword">
<soap:operation soapAction="http://tempuri.org/IScauCSService/GetMyPassword" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetUPCA">
<soap:operation soapAction="http://tempuri.org/IScauCSService/GetUPCA" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="CheckStudentPassword">
<soap:operation soapAction="http://tempuri.org/IScauCSService/CheckStudentPassword" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ScauCSService">
<wsdl:port name="BasicHttpBinding_IScauCSService" binding="tns:BasicHttpBinding_IScauCSService">
<soap:address location="http://t.vip315.net:8800/ScauCsService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
从文档我们可以得到的信息有:
(1)该WebSerivce所基于的SOAP协议版本是SOAP1.2
(2)该WebSerivce的命名空间(NameSpace)是http://tempuri.org
(3)我们所要调用的方法为GetMyPassword,而该方法所对应的方法的前缀为IScauCSService
(4)代码实现
package com.main;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.job2.R;
import com.util.MD5Util;
public class MainActivity extends Activity {
private Button btn;
private TextView resultView;
private String result;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
resultView = (TextView)findViewById(R.id.textView);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
String studentNum = "201131000627";
String pass = "998179";
result = getRemoteInfo(studentNum, pass);
handler.sendEmptyMessage(0);
}
}.start();
}
});
}
private String getRemoteInfo(String studentNum, String pass){
// 命名空间
String nameSpace = "http://tempuri.org/";
// 调用的方法名称
String methodName = "GetMyPassword";
// EndPoint
String endPoint = "http://t.vip315.net:8800/ScauCsService.svc";
// SOAP Action
String soapAction = "http://tempuri.org/IScauCSService/GetMyPassword";
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的两个参数
rpc.addProperty("strStudentNumber", studentNum);
rpc.addProperty("strPassWordMD5", MD5Util.string2MD5(pass));
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
transport.debug = true;
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
resultView.setText(result);
};
};
}
(5)开发过程中的异常处理过程
(1)android.os.NetworkOnMainThreadException 异常处理
<uses-permission android:name="android.permission.INTERNET" />