android: 调用webservice

(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 异常处理

产生原因: android4.0以上访问网络的功能不能发生在主线程中运行,本程序原来是在主线程中调用webservice的,所以产生异常
解决方法: Thread + Handler ,创建新进程来访问webservice
(2)java.net.SocketException: Permission denied (maybe missing INTERNET permission) 错误
产生原因:需要访问到网络,但Socket不能对外连接,错误不会被报出,调试的时候,能看到Exception。关于这个错误可访问http://www.myexception.cn/android/736848.html
解决方法:在AndroidManifest.xml中,需要进行如下配置:在 </application>下添加一行
	<uses-permission android:name="android.permission.INTERNET" />

(3)java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject 错误
解决方法:(a)右键项目,依次选择properties ---- java build path -----order and report ,然后在koasp2包前面打上勾 ,
    (b)project - clean 








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值