Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将复杂对象传递给web服务_Android_Web Services_Ksoap2 - Fatal编程技术网

Android 将复杂对象传递给web服务

Android 将复杂对象传递给web服务,android,web-services,ksoap2,Android,Web Services,Ksoap2,我正在尝试从我的应用程序调用web服务。如果我调用接受字符串或原语作为输入的web服务方法,那么它可以正常工作,但是当调用接受复杂对象(在我的例子中是样本类)的web服务方法时,我会得到以下错误 org.apache.axis2.AxisFault: Unknow type {http://spec.com}specimen at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:349)

我正在尝试从我的应用程序调用web服务。如果我调用接受字符串或原语作为输入的web服务方法,那么它可以正常工作,但是当调用接受复杂对象(在我的例子中是样本类)的web服务方法时,我会得到以下错误

org.apache.axis2.AxisFault: Unknow type {http://spec.com}specimen
    at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:349)
    at org.apache.axis2.databinding.utils.BeanUtil.processObject(BeanUtil.java:827)
    at org.apache.axis2.databinding.utils.BeanUtil.ProcessElement(BeanUtil.java:746)   
我的发言如下:

我已经在web服务浏览器中测试了我的web服务,它运行良好

package com.spec;

public class Spec {

    public String sayHello(String name)   // I can successfully invoke this 
    {
        return "Have a great day " + name;
    }

    public String saveSpecimen(Specimen specimen) // Getting error for this
    {
        System.out.println("id: " + specimen.getId());
        System.out.println("name: " + specimen.getName());
        return "Specimend with id " + specimen.getId() + " is successfully saved";
    }
}
这是我的样本课

package com.ws;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Specimen implements KvmSerializable {
    int id;
    String name;

    public Specimen() {
    }

    public Specimen(int idValue, String nameValue) {
        id = idValue;
        name = nameValue;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getProperty(int arg0) {
        if (arg0 == 0)
            return id;
        else
            return name;
    }

    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
        switch (arg0) {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "id";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "name";
            break;
        default:
            break;
        }
    }

    public void setProperty(int arg0, Object value) {
        switch (arg0) {
        case 0:
            id = Integer.parseInt(value.toString());
            break;
        case 1:
            name = value.toString();
            break;
        }
    }
}   
活动的片段

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);

        Specimen specimen = new Specimen();
        specimen.setId(1);
        specimen.setName("Test_Specimen");
        PropertyInfo info = new PropertyInfo();
        info.setName("specimen");
        info.setValue(specimen);
        info.setType(specimen.getClass());
        request.addProperty(info);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        envelope.addMapping(WSDL_TARGET_NAMESPACE, "specimen",new Specimen().getClass());
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject)envelope.getResponse();
            Toast.makeText(getApplicationContext(),"Msg from web servce "       +              response.toString(),Toast.LENGTH_LONG).show();
我想问题出在你身上

 envelope.addMapping(WSDL_TARGET_NAMESPACE, "specimen",new Specimen().getClass());
下面是我在wsdl文件中的复杂类型声明示例

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" argetNamespace="http://spec.com/xsd">   
<xs:complexType name="Specimen">  
<xs:sequence>  
<xs:element minOccurs="0" name="id" type="xs:int"/>   
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/></xs:sequence></xs:complexType>  
</xs:schema>

如果我犯了什么错误,请告诉我

谢谢

试试这个:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Specimen", specimen);
SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping("http://spec.com/xsd", "Specimen", new Specimen().getClass());

try {
    HttpTransportSE transport =  new HttpTransportSE(SOAP_ADDRESS);
    transport.call(NAMESPACE + METHOD_NAME, envelope);
    Object response = envelope.getResponse();
    String result = response.toString();
    /* make a toast ... */
} catch (Exception e) {
    e.printStackTrace();
}
如果不起作用,请发布您的WSDL\u目标\u命名空间、操作\u名称和地址字符串。

尝试以下操作:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Specimen", specimen);
SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping("http://spec.com/xsd", "Specimen", new Specimen().getClass());

try {
    HttpTransportSE transport =  new HttpTransportSE(SOAP_ADDRESS);
    transport.call(NAMESPACE + METHOD_NAME, envelope);
    Object response = envelope.getResponse();
    String result = response.toString();
    /* make a toast ... */
} catch (Exception e) {
    e.printStackTrace();
}

如果它不起作用,请发布您的WSDL\u目标\u名称空间、操作\u名称和地址字符串。

尝试了很多,但都没有成功。所以,将web服务更新为简单的参数,并传递对象中存在的所有属性。

尝试了很多,但无法使其工作。因此,将web服务更新为简单参数并传递对象中存在的所有属性。

尝试使其成为大写信封。addMapping(WSDL_TARGET_名称空间,“sample”,new sample().getClass());看看它是否有效?我不确定。@hrishikeshp19:运气不佳..您访问过这里吗->?尝试制作大写信封.addMapping(WSDL_TARGET_名称空间,“sample”,new sample().getClass());看看它是否有效?我不确定。@hrishikeshp19:没有运气。.你访问过这里吗->?尝试过你的建议,但没有运气得到
ArrayIndexOutofBoundExeption
。可能是错误妨碍了我得到响应,请尝试更改该行。我想其余的都没问题。@enmarc:我不认为
Envelope.getResponse
会导致
arrayIndexOutofBoundException
尝试了你的建议,但没有成功获得
arrayIndexOutofBoundException
。可能是错误妨碍了我获得响应,尝试更改那一行。我认为其余的都可以。@enmarc:我不认为
Envelope.getResponse
会导致
arrayIndexOutofBoundException