android调用webservice

Android SDK本身并不提供调用Webservice的API,要想调用Webservice,需要使用第三方的API,如KSOAP2。

使用KSOAP2调用Webservice,步骤如下:

1 指定Webservice的命名空间和调用的方法名

SoapObject request = new SoapObject("http://service","getName");

SoapObject类的第1个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第2个参数表示调用WebService的方法名。

2 设置调用方法的参数值,这一步是可选的,如果方法没有参数,可以省略该步。

request.addProperty("param1","value1"); 
request.addProperty("param2","value2");

要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。

3 生成调用WebService方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述。

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelop.VER11);
envelope.bodyOut = request;

创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。该版本号需要根据服务端WebService的版本号设置。在创建SoapSerializationEnvelope对象后,不要忘了设置SoapSerializationEnvelope类的bodyOut属性,该属性的值就是在第1步创建的SoapObject对象。

4 创建HttpTransportSE对象。通过HttpTransportSE类的构造方法可以指定WebService的WSDL文档的URL。

HttpTransportSE ht = new HttpTransportSE("http://127.0.0.1/services/webservice?wsdl");

5 使用call方法调用WebService方法。

ht.call(null,envelope);

call方法的第1个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象。

6 使用getResponse方法获得WebService方法的返回结果。

SoapObject soapObject = (SoapObject) envelope.getResponse();

7 如果WebService方法返回了对象,可以使用SoapObject.getProperty方法获取属性值。

String name = soapObject.getProperty("name");

完成代码

private void getDataFromWebService(String caseId){
        // 命名空间  
        String nameSpace = "http://ws.prc.jda.com/";  
        // 调用的方法名称  
        String methodName = "getBottlingDateByCase";
        // EndPoint  
        String endPoint = webServiceUrl;//"https://192.168.37.43:8777/TraceabitityWS/services/PDAPrintService"  

        // SOAP Action  
        String soapAction = "http://ws.prc.jda.com/getBottlingDateByCase";  
        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);  

        // 设置需调用WebService接口需要传入的参数 
        rpc.addProperty("caseId", caseId);  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10)  ;

        envelope.bodyOut = rpc;  
        // 设置是否调用的是dotNet开发的WebService  
     //   envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);  

        try {  
            HttpTransportSE transport = new HttpTransportSE(endPoint,(timeOut*1000)/2);
            // https 认证
            SSLConection.allowAllSSL(); 
            // 调用WebService
            transport.call(soapAction, envelope);  
          // 获取返回的数据  
          SoapObject object = (SoapObject) envelope.bodyIn;

            // 获取返回的结果  
          System.out.println("返回的结果"+object);
          if(object.getPropertyCount() > 0 ){
              for(int i = 0; i < object.getPropertyCount(); i++){
                  SoapObject details = (SoapObject) object.getProperty(i);
                  String date = "";
                  String count = "";
                  for(int j = 0; j < details.getPropertyCount()-1; j++){
                      date = details.getPropertyAsString(j);
                      count = details.getPropertyAsString(j+1);
                  }
                  mDatas.add(new PrintData(date, count+"瓶"));
              }
              handler.sendEmptyMessage(SUCCESS);
          }else{
              handler.sendEmptyMessage(NULL);
          }
        } catch(SocketTimeoutException e){
            e.printStackTrace();
            handler.sendEmptyMessage(TIMEOUT);
        } catch (IOException e) {
            doException(e);
        } catch (XmlPullParserException e) {
            doException(e);
        }catch (ClassCastException e) {
            doException(e);
        }
    }
import android.util.Log;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class SSLConection {

    private static TrustManager[] trustManagers;

    public static class _FakeX509TrustManager implements javax.net.ssl.X509TrustManager {
        private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

        public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return (_AcceptedIssuers);
        }
    }

    public static void allowAllSSL() {

        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        javax.net.ssl.SSLContext context;

        if (trustManagers == null) {
            trustManagers = new TrustManager[]{new _FakeX509TrustManager()};
        }

        try {
            context = javax.net.ssl.SSLContext.getInstance("TLS");
            context.init(null, trustManagers, new SecureRandom());
            javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            Log.e("allowAllSSL", e.toString());
        } catch (KeyManagementException e) {
            Log.e("allowAllSSL", e.toString());
        }
    }
}

KSOAP2下载地址:http://download.csdn.net/detail/u011115385/9400358

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值