本文介绍了KSOAP2为Android提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图访​​问一个Web服务的WSDL是在 http://srilanka.lk :9080 /服务/ CropServiceProxy WSDL ?使用了SoapUI我发送一个请求,并成功地得到了回应。

然后用KSoap2为Android我试图得到回应。但我得到的是一个错误的SOAPFault。在code是如下。

字符串NAMESPACE =htt​​p://schemas.icta.lk/xsd/crop/handler/v1;

 字符串的URL =htt​​p://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint;

    字符串方法_ =getCropDataList;
    字符串SOAP_ACTION =方法_;

    SoapObject请求=新SoapObject空间(namespace,方法_);
    字符串crop_ code_str =将String.valueOf(code.getText()的toString());
    的System.out.println(crop_ code_str);
    的System.out.println(request.toString());
    request.addProperty(code,crop_ code_str);
    的System.out.println(request.getProperty(code)的toString());

    SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(要求);

    的System.out.println(体而出:+ envelope.bodyOut.toString());

    HttpTransportSE http_transport =新HttpTransportSE(URL);
    尝试 {
        http_transport.call(SOAP_ACTION,包);
        的System.out.println(envelope.bodyIn.toString());

    }赶上(例外五){
        e.printStackTrace();
        answer.setText(错误捕获);
    }
 

这导致了以下

  W / System.err的(394):org.xmlpull.v1.XmlPullParserException:期望值START_TAG {http://www.w3.org/2001/12/soap-envelope }信封(位置:START_TAG< {http://schemas.xmlsoap.org/soap/envelope/} soapenv:信封> @ 1:java.io.InputStreamReader@43e81258 114)
 

我试着用VER11但随后提供了以下错误的bodyIn。

  I /的System.out(365):的SOAPFault  - 故障code:'soapenv:服务器在faultstring:org.apache.axis2.databinding.ADBException:意外的子元素code'faultactor:'空'的细节:org.kxml2.kdom.Node@43e84648
 

这是什么毛病Ksoap2为Android?然后,有另一种方式,一个Web服务可以在Android的消耗? (仅SOAP可用)

更新:我试过了KvmSerializable接口,为complext类型。以下是我的code。我仍然得到同样的错误。该XMLPullParser错误被抛出在http_transport.call行。

这是复杂类型​​的实现在客户端。我只有WSDL。

 公共类CropInfo实现KvmSerializable {

    私人字符串名称;
    私人浮动价格;
    私人字符串的位置;

    @覆盖
    公共对象的getProperty(INT为arg0){
        开关(将arg0){
        情况下0:
            返回名称;
        情况1:
            返回的价格;
        案例2:
            返回的位置;
        默认:
                返回null;
        }
    }

    @覆盖
    公众诠释getPropertyCount(){
        返回3;
    }

    @覆盖
    公共无效为getPropertyInfo(INT为arg0,哈希表ARG1,ARG2的PropertyInfo){
        开关(将arg0){
        情况下0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name =名称;
            打破;
        情况1:
            arg2.type = Float.class;
            arg2.name =价格;
            打破;
        案例2:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name =位置;
            打破;
        默认:
                打破;
        }

    }

    @覆盖
    公共无效的SetProperty(INT为arg0,对象ARG1){
        开关(将arg0){
        情况下0:
            NAME = arg1.toString();
            打破;
        情况1:
            价格= Float.parseFloat(arg1.toString());
        案例2:
            位置= arg1.toString();
        默认:
            打破;
        }
    }
}
 

的修饰的code键访问和解析Web服务的响应

字符串NAMESPACE =htt​​p://schemas.icta.lk/xsd/crop/handler/v1/;        字符串的URL =htt​​p://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint;        字符串方法_ =getCropDataList;        字符串SOAP_ACTION =htt​​p://schemas.icta.lk/xsd/crop/handler/v1/getCropDataList;

  SoapObject soap_request =新SoapObject空间(namespace,方法_);
    字符串crop_ code_str =将String.valueOf(code.getText()的toString());
    的System.out.println(crop_ code_str);
    的System.out.println(soap_request.toString());
    soap_request.addProperty(code,crop_ code_str);
    的System.out.println(soap_request.getProperty(code)的toString());

    SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(soap_request);
    envelope.addMapping(命名空间cropInfo,CropInfo.class);
    //envelope.dotNet=true;

    元帅floatMarshal =新MarshalFloat();
    floatMarshal.register(信封);

    的System.out.println(体而出:+ envelope.bodyOut.toString());

    // AndroidHttpTransport http_transport =新AndroidHttpTransport(URL);
    HttpTransportSE http_transport =新HttpTransportSE(URL);
    尝试 {
                //错误来自这个调用
                http_transport.call(SOAP_ACTION,包);


        矢量< CropInfo> result_array =(矢量< CropInfo&GT)envelope.getResponse();
        如果(result_array!= NULL){
            对于(CropInfo current_crop:result_array){
                的System.out.println(current_crop.getName());
                的System.out.println(Float.toString(current_crop.getPrice()));
            }
        }

    }赶上(例外五){
        e.printStackTrace();
        answer.setText(错误捕获);
    }
 

解决方案

我觉得你的code就可以了。

渴看一下您的WSDL文件,在这里你可以找到你需要等所有数据,如namspace,采取行动。在案件的行动很多像,一个emty字符串。我总是选择满足SoapEnvelope.VER11,但normaly没关系。这code只有primitiv数据类型和String数组工作,如果你希望复杂的数据,你必须实现kvmSerializable接口。

对不起我的英文不好,我希望你明白我的意思。下面是一个有用的链接。

I'm trying to access a web service whose WSDL is at http://srilanka.lk:9080/services/CropServiceProxy?wsdl . Using SoapUI I sent a request and successfully got a response.

Then using KSoap2 for Android I tried to get a response. But all I get is a SoapFault Error. The code is as follows.

String NAMESPACE = "http://schemas.icta.lk/xsd/crop/handler/v1";

    String URL = "http://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint";

    String method_name = "getCropDataList";
    String SOAP_ACTION = method_name;

    SoapObject request = new SoapObject(NAMESPACE, method_name);
    String crop_code_str = String.valueOf(code.getText().toString());
    System.out.println(crop_code_str);
    System.out.println(request.toString());
    request.addProperty("code", crop_code_str );
    System.out.println(request.getProperty("code").toString());

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(request);

    System.out.println("body out : " + envelope.bodyOut.toString());

    HttpTransportSE http_transport  = new HttpTransportSE(URL);
    try {
        http_transport.call(SOAP_ACTION, envelope);
        System.out.println(envelope.bodyIn.toString());

    } catch (Exception e) {
        e.printStackTrace();
        answer.setText("error caught");
    }

This results in the following.

W/System.err(  394): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://www.w3.org/2001/12/soap-envelope}Envelope (position:START_TAG <{http://schemas.xmlsoap.org/soap/envelope/}soapenv:Envelope>@1:114 in java.io.InputStreamReader@43e81258)

I tried with VER11 but it then gives the following error as the bodyIn.

I/System.out(  365): SoapFault - faultcode: 'soapenv:Server' faultstring: 'org.apache.axis2.databinding.ADBException: Unexpected subelement code' faultactor: 'null' detail: org.kxml2.kdom.Node@43e84648

Is this something wrong with Ksoap2 for android? Then is there another way that a web service can be consumed in Android? (Only SOAP is available)

UPDATE: I tried the KvmSerializable interface for the complext type. Following is my code. I still get the same errors. The XMLPullParser error is being thrown at the http_transport.call line.

This is the complex type implementation at the client side. I have only the WSDL.

public class CropInfo implements KvmSerializable {

    private String name;
    private float price;
    private String location;

    @Override
    public Object getProperty(int arg0) {
        switch (arg0){
        case 0:
            return name;
        case 1:
            return price;
        case 2:
            return location;
        default:
                return null;
        }
    }

    @Override
    public int getPropertyCount() {
        return 3;
    }

    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
        switch (arg0){
        case 0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Name";
            break;
        case 1:
            arg2.type = Float.class;
            arg2.name = "Price";
            break;
        case 2:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Location";
            break;
        default:
                break;
        }

    }

    @Override
    public void setProperty(int arg0, Object arg1) {
        switch(arg0){
        case 0:
            name = arg1.toString();
            break;
        case 1:
            price = Float.parseFloat(arg1.toString());
        case 2:
            location = arg1.toString();
        default:
            break;
        }
    }
}

The modified code to access and parse the response of the web service.

String NAMESPACE = "http://schemas.icta.lk/xsd/crop/handler/v1/"; String URL = "http://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint"; String method_name = "getCropDataList"; String SOAP_ACTION = "http://schemas.icta.lk/xsd/crop/handler/v1/getCropDataList";

    SoapObject soap_request = new SoapObject(NAMESPACE, method_name);
    String crop_code_str = String.valueOf(code.getText().toString());
    System.out.println(crop_code_str);
    System.out.println(soap_request.toString());
    soap_request.addProperty("code", crop_code_str );
    System.out.println(soap_request.getProperty("code").toString());

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(soap_request);
    envelope.addMapping(NAMESPACE, "cropInfo", CropInfo.class);
    //envelope.dotNet=true;

    Marshal floatMarshal = new MarshalFloat();
    floatMarshal.register(envelope);

    System.out.println("body out : " + envelope.bodyOut.toString());

    //AndroidHttpTransport http_transport = new AndroidHttpTransport(URL);
    HttpTransportSE http_transport  = new HttpTransportSE(URL);
    try {
                //error comes from this call
                http_transport.call(SOAP_ACTION, envelope);


        Vector<CropInfo> result_array = (Vector<CropInfo>)envelope.getResponse();
        if(result_array != null){
            for (CropInfo current_crop: result_array){
                System.out.println(current_crop.getName());
                System.out.println(Float.toString(current_crop.getPrice()));
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        answer.setText("error caught");
    }
解决方案

i think your code is ok.

Thirst have a look at your wsdl file, here you can find all the data you need, like namspace, action an so on. In alot of cases action is an emty string like "".I always choos SoapEnvelope.VER11, but normaly it doesn't matter.This code only works with primitiv datatypes and String arrays if you expect complex data you must implement the kvmSerializable interface.

Sorry for my bad english, i hope you understand what i mean. Below is a helpful link.

http://www.drdobbs.com/mobility/208800166;jsessionid=2VAK4K4QQ5H3TQE1GHPSKH4ATMY32JVN?pgno=1

这篇关于KSOAP2为Android提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:02