java 调用asp.net webservice 接口 获取json,提交json,map数据格式

下载一个jar包:ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar


定义webservice 方法


调用:



java代码:

package com.qsmart.audit.utility;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

/**
 * 调用Asp.Net web services
 * 
 * @author 李波
 * 
 * @date 2013-11-08
 */
public class HttpClientHelper
{
	private final String NAMESPACE = "http://tempuri.org/";

	private String url = null;

	private String methodName = null;

	private String soapAction = null;

	public HttpClientHelper(String url, String methodName)
	{
		this.url = url;
		this.methodName = methodName;
		this.soapAction = NAMESPACE + methodName;
	}

	/**
	 * DoGet
	 * 
	 * @param map
	 * @return
	 */
	public String HttpDoGet(Map<String, String> map)
	{

		String resultString = "";
		try
		{
			HttpTransportSE httpTransportSE = new HttpTransportSE(url);
			httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
			Set<Map.Entry<String, String>> entryseSet = map.entrySet();
			for (Map.Entry<String, String> entry : entryseSet)
			{
				soapObject.addProperty(entry.getKey(), entry.getValue());
			}
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);
			httpTransportSE.call(soapAction, envelope);
			if (envelope.getResponse() != null)
			{
				Object response = (Object) envelope.getResponse();
				resultString = response.toString();
			}
		} catch (Exception e)
		{
			resultString = e.getMessage();
		}
		return resultString;
	}

	/**
	 * 提交数据
	 * 
	 * @param map
	 *            map数据格式
	 * @return true or false
	 */
	public boolean HttpDoPostByMap(Map<String, String> map)
	{
		boolean result = false;
		HttpTransportSE transport = new HttpTransportSE(url);
		transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		transport.debug = true;
		SoapObject request = new SoapObject(NAMESPACE, methodName);
		Set<Map.Entry<String, String>> entryseSet = map.entrySet();

		for (Map.Entry<String, String> entry : entryseSet)
		{
			request.addProperty(entry.getKey(), entry.getValue());
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.bodyOut = transport;
		envelope.setOutputSoapObject(request);
		try
		{
			transport.call(soapAction, envelope);
			if (envelope.getResponse() != null)
			{
				Object obj = (Object) envelope.getResponse();
				Boolean rBoolean = Boolean.parseBoolean(obj.toString());
				if (rBoolean == true)
				{
					result = true;
				}
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return result;
	}

	/**
	 * 提交数据
	 * 
	 * @param json
	 *            json数据格式
	 * @return true or false
	 */
	public boolean HttpDoPostByJson(String json)
	{
		boolean result = false;
		HttpTransportSE transport = new HttpTransportSE(url);
		transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		transport.debug = true;
		SoapObject request = new SoapObject(NAMESPACE, methodName);
		request.addProperty("json", json);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.bodyOut = transport;
		envelope.setOutputSoapObject(request);
		try
		{
			transport.call(soapAction, envelope);
			if (envelope.getResponse() != null)
			{
				Object obj = (Object) envelope.getResponse();
				Boolean rBoolean = Boolean.parseBoolean(obj.toString());
				if (rBoolean == true)
				{
					result = true;
				}
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return result;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值