android ksoap2调用.net Webservice 方法总结

android ksoap2调用.net Webservice 方法直接放到一个类里:

package com.util;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;



public class AsmxUtil 
{
	private String SOAP_ACTION = "";
	private String NAMESPACE = "http://tempuri.org/";
	private String METHOD_NAME = "";
	private String ServiceURL = "";
	SoapObject request =null;
	
	public AsmxUtil(String serviceurl, String methodName)
	{
		
		// 建立webservice连接对象  
		ServiceURL = serviceurl;
		METHOD_NAME = methodName;
		SOAP_ACTION = NAMESPACE + METHOD_NAME;
		request = new SoapObject(NAMESPACE, METHOD_NAME); 
	}
	
	public void AddProperty(String name, Object v)
	{
		//参数值
		request.addProperty(name, v);
	  
	}
	
	public String Call() throws Exception
	{
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		// soap协议版本必须用SoapEnvelope.VER11(Soap V1.1) 
    	envelope.bodyOut = request;
    	envelope.dotNet=true;
    	//对dotnet webservice协议的支持,如果dotnet的webservice 
    	//不指定rpc方式则用true否则要用false 
    	
    	HttpTransportSE ht = new HttpTransportSE(ServiceURL);
    	ht.debug = true;
    	try {
    		ht.call(SOAP_ACTION, envelope);
    		if (envelope.getResponse() != null) 
    		{
    			String msg = envelope.getResponse().toString();
    			return msg;
    		} 
    		else 
    		{
    			return "error";
    		}
    	}
		catch (Exception e) {
    		throw new Exception(e.getMessage());
    
		}
	}
}

调用方法:

AsmxUtil au = new AsmxUtil("http://192.168.111.153/WebService2/Service1.asmx", "HelloWorld");
au.AddProperty("username","admin");
au.AddProperty("password","123");
String xst = null;
try {
xst = au.Call();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//text1.setText(e.getMessage());
}

Toast.makeText(this, xst, Toast.LENGTH_LONG).show();




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值