ITeye博客

用Soap调用webservice接口

youchengLin · 2015-06-29 14:51 · 移动开发 · Markdown

package com.my.test;

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

public class WebserviceAxis { public static void main(String args[]) throws Exception { try { // 命名空间
String nameSpace = "http://tempuri.org/";
// 调用的方法名称
String methodName = "ScoreIt";
// EndPoint
String endPoint = "http://gzonline.gov.cn/nsws/EvaluateUpdate.asmx";
// SOAP Action
String soapAction = "http://tempuri.org/ScoreIt";

        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);  
  
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        rpc.addProperty("ServiceID", "ser0005");  //不能重复
        rpc.addProperty("UserID", "user0001");  
        rpc.addProperty("itemScore", 1);  
        rpc.addProperty("detailIScore", 0);  
  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  
  
        envelope.bodyOut = rpc;  
        // 设置是否调用的是dotNet开发的WebService  
        envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);  
  
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
        try {  
            // 调用WebService  
            transport.call(soapAction, envelope);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        // 获取返回的数据  
        SoapObject object = (SoapObject) envelope.bodyIn;  
        // 获取返回的结果  
        String result = object.getProperty(0).toString();  
        
        System.out.println(result);
        // 将WebService返回的结果显示在TextView中  
        //resultView.setText(result);  


    } catch (Exception e) {

          //java.io.InterruptedIOException: Read timed out
    	  e.printStackTrace();
          System.out.println(e.getMessage());

    }

} }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。