Java中Soap的几种使用方法(android 可用)

本文章使用的是http://www.webxml.com.cn/WebServices/WeatherWebService.asmx中的webservice中的soap使用方法。

  • SOAP使用方法一:

本方法使用Java中自带的函数,使用比较简单,具体如下:
a.首先需要准备2个文件:soap1.1.xml soap1.2.xml 分别对应两种协议格式文件,放到程序根目录即可。
soap1.1.xml的内容如下:

<?xml version=“1.0″ encoding=“utf-8″?>
<soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Body>
<getWeatherbyCityName xmlns=“http://WebXml.com.cn/”>
<theCityName>beijing</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>
 

soap1.2.xml内容如下:

<?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>
<getWeatherbyCityName xmlns=“http://WebXml.com.cn/”>
<theCityName>beijing</theCityName>
</getWeatherbyCityName>
</soap12:Body>
</soap12:Envelope>

b.好了,准备这两个文件后,继续的JAVA代码如下:

import java.io.*;
import java.net.*;
import javax.xml.soap.*;
public class Usesoap12 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MessageFactory msgfac =MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);//Soap 1.2版
SOAPMessage message=msgfac.createMessage();//用信息工厂生产一个Soap信息
SOAPPart sp = message.getSOAPPart();
SOAPEnvelope se = sp.getEnvelope();
//SOAPBody sb = se.getBody();
//SOAPHeader sh = se.getHeader();
sp.setMimeHeader(“Content-Type”, “application/soap+xml; charset=utf-8″);
//设置命名空间
se.setAttributeNS(“http://www.w3.org/2001/XMLSchema-instance”, “xsi”,null);
se.setAttributeNS(“http://www.w3.org/2001/XMLSchema”, “xsd”,null);
//se.setAttributeNS(“http://schemas.xmlsoap.org/soap/envelope/”, ”soap”,null);//soap1.1
se.setAttributeNS(“http://www.w3.org/2003/05/soap-envelope”, “soap”,null);//soap1.2
String urlString = “http://www.webxml.com.cn/WebServices/WeatherWebService.asmx”;
String xmlFile = “soap1.1.xml”;//要发送的soap格式文件
//String soapActionString = ”http://WebXml.com.cn/getWeatherbyCityNamePro”;//Soap 1.1中使用
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();;
File fileToSend=new File(xmlFile);
byte[] buf=new byte[(int)fileToSend.length()];//用于存放文件数据的数组
new FileInputStream(xmlFile).read(buf);
httpConn.setRequestProperty( “Content-Length”,String.valueOf( buf.length ) );
httpConn.setRequestProperty(“Content-Type”,“text/xml; charset=utf-8″);
//httpConn.setRequestProperty(“soapActionString”,soapActionString);//Soap 1.1中使用
httpConn.setRequestMethod( “POST” );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write( buf );
out.close();
InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),“utf-8″);
BufferedReader in = new BufferedReader(is);
String inputLine;
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(“result.xml”)));//将结果存放的位置
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
bw.write(inputLine);
bw.newLine();
}
bw.close();
in.close();
httpConn.disconnect();
}
}

注:上面的代码是针对soap1.2的,如果想使用1.1版,把对应1.1的注释去掉,把1.2的注释掉即可~

 

  • SOAP使用方法二:ksoap-android的使用

ksoap-android下载

//返回结果字符串是一个xml格式的,即对应的soap响应结果
public static String getCityList() {
// 命名空间
String serviceNameSpace = “http://WebXml.com.cn/”;
// 请求URL
String serviceURL = “http://www.webxml.com.cn/WebServices/WeatherWebService.asmx”;
// 调用的方法
String methodName = “getSupportProvince”;
// 实例化序列化的Envelop
SoapObject request = new SoapObject(serviceNameSpace, methodName);
// 获得序列化的Envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = request;
(new MarshalBase64()).register(envelope);
// Android传输对象
AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);
ht.debug = true;
try {
ht.call(“http://WebXml.com.cn/getSupportProvince”, envelope);
if (envelope.getResponse() != null) {
return envelope.bodyIn.toString();
}
} catch (IOException e) {
e.toString();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}



转载地址:http://pcodenote.com/study-note/use-soap-in-java-android/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值