android调用webservice接口应用实例

public String userLogin(String userName, String pwd) {
        String url = "http://xx.xx.com/services/User";
        String nameSpace = "http://xx.xx.com";
        String SOAP_ACTION  = "http:/xx.xx.com/services/User/login";
        String method = "login";
        String result = "";
        SoapObject request = new SoapObject(nameSpace, method);
        
        request.addProperty("nickname", userName);
        request.addProperty("password", pwd);
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE ht = new HttpTransportSE(url);
        try {
            ht.call(SOAP_ACTION, envelope);
            Object object = envelope.getResponse();
            result = object.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    } 

一,需要注意的是SOAP_ACTION可以有可无。

二,有时候会报java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误,解决方法如下:

以下内容来自:http://blog.csdn.net/whybiang/article/details/6533051

在服务器端返回值是String类型的数值的时候使用SoapObject soapObject = (SoapObject) envelope.getResponse();和 SoapObject result = (SoapObject)envelope.bodyIn;这两种方法来接受值都会报出 

java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive这样的错误。

我们可以使用 Object object = envelope.getResponse();就可以解决这种错误。 

 如果服务器返回值的类型是byte[] 的时候,使用Object object = envelope.getResponse();和SoapObject result = (SoapObject)envelope.bodyIn;

都不会发生错误现象,但是在使用Object object = envelope.getResponse();取回来的值在使用base64进行解码和编码的时候会报出错误。

如果使用SoapObject result = (SoapObject)envelope.bodyIn;就可以完整的将byte[]进行解码和编码

,byte[] ops = Base64.decode(result.getProperty(0).toString());至于在用SoapObject soapObject = (SoapObject) 

envelope.getResponse();来接受byte[]返回值,由于时间关系我没有进行测试

posted @ 2012-06-14 12:09  fighter  阅读(5253)  评论(0编辑  收藏  举报