Getting a nullreference when passing a complex object from KSOAP2 to WCF

荒凉一梦 提交于 2019-12-13 03:58:06

问题


So guys I know that there exist a lot of similar question in here, but none of them have been able to resolve my issue.

I’m trying to send a custom Object User from an Android device to a WCF service, but no matter what I do the webservice receive a null.

Here's the code for the object im trying to send:

public class User implements KvmSerializable {

private int id; 
private String userName;
private String mac;
private int proppertyCount = 3;


public User(int _id, String _userName, String _mac)
{
    this.id =_id;
    this.userName = _userName;
    this.mac = _mac;
}

public User()
{

}


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getMac() {
    return mac;
}
public void setMac(String mac) {
    this.mac = mac;
}

@Override
public Object getProperty(int arg0) {

    switch(arg0)
    {
        case 0: return id;

        case 1: return userName;

        case 2: return mac;

    }
    return null;
}

@Override
public int getPropertyCount() {

    return proppertyCount;
}

@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {

    switch(index)
    {
    case 0:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "CategoryId";
        break;
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "Name";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "Description";
        break;
    default:break;
    }

}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case 0:
        id = Integer.parseInt(value.toString());
        break;
    case 1:
        userName = value.toString();
        break;
    case 2:
        mac = value.toString();
        break;
    default:
        break;
    }

}

My method to pass the object through ksoap2:

        private static String SOAP_ACTION = "http://nordahl.org/IWcfFoodAndAllergi/";
    private static  String METHOD_NAME;
    private static final String NAMESPACE = "http://nordahl.org/";
    private static final String URL = "http://192.168.0.18/WcfFoodAndAllergi.svc";

    public void InsertUser(User _user)
    {

        METHOD_NAME = "InsertUser"; 
        SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
        SoapObject result;
        int retInt = -1;

            SoapObject req = new SoapObject(NAMESPACE, METHOD_NAME);



            PropertyInfo p = new PropertyInfo();

            p.setName("User");
            p.setNamespace(NAMESPACE);
            p.setValue(_user);
            p.setType(_user.getClass());
            p.setType(_user.getClass());

            req.addProperty(p);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.implicitTypes = true;
            envelope.setAddAdornments(false);
            envelope.setOutputSoapObject(req);    

            envelope.addMapping(NAMESPACE, "User", new User().getClass());

            HttpTransportSE ht = new HttpTransportSE(URL); 

            try{
                    ht.debug= true;
                    ht.call(SOAP_ACTION, envelope);
                    result = (SoapObject)envelope.getResponse();
            }
            catch (Exception e)
            {
                e.printStackTrace();


            }
    }

And finally my wsdl file:

    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://nordahl.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="WcfFoodAndAllergi" targetNamespace="http://nordahl.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://nordahl.org/Imports">
<xsd:import schemaLocation="http://192.168.0.18/WcfFoodAndAllergi.svc?xsd=xsd0" namespace="http://nordahl.org/"/>
<xsd:import schemaLocation="http://192.168.0.18/WcfFoodAndAllergi.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://192.168.0.18/WcfFoodAndAllergi.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/ModelLayer"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IWcfFoodAndAllergi_InsertUser_InputMessage">
<wsdl:part name="parameters" element="tns:InsertUser"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_InsertUser_OutputMessage">
<wsdl:part name="parameters" element="tns:InsertUserResponse"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetUser_InputMessage">
<wsdl:part name="parameters" element="tns:GetUser"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetUser_OutputMessage">
<wsdl:part name="parameters" element="tns:GetUserResponse"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_InsertRecipe_InputMessage">
<wsdl:part name="parameters" element="tns:InsertRecipe"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_InsertRecipe_OutputMessage">
<wsdl:part name="parameters" element="tns:InsertRecipeResponse"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByAllergi_InputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByAllergi"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByAllergi_OutputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByAllergiResponse"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByType_InputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByType"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByType_OutputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByTypeResponse"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByName_InputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByName"/>
</wsdl:message>
<wsdl:message name="IWcfFoodAndAllergi_GetRecipeByName_OutputMessage">
<wsdl:part name="parameters" element="tns:GetRecipeByNameResponse"/>
</wsdl:message>
<wsdl:portType name="IWcfFoodAndAllergi">
<wsdl:operation name="InsertUser">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/InsertUser" message="tns:IWcfFoodAndAllergi_InsertUser_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/InsertUserResponse" message="tns:IWcfFoodAndAllergi_InsertUser_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetUser">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetUser" message="tns:IWcfFoodAndAllergi_GetUser_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetUserResponse" message="tns:IWcfFoodAndAllergi_GetUser_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="InsertRecipe">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/InsertRecipe" message="tns:IWcfFoodAndAllergi_InsertRecipe_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/InsertRecipeResponse" message="tns:IWcfFoodAndAllergi_InsertRecipe_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetRecipeByAllergi">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByAllergi" message="tns:IWcfFoodAndAllergi_GetRecipeByAllergi_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByAllergiResponse" message="tns:IWcfFoodAndAllergi_GetRecipeByAllergi_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetRecipeByType">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByType" message="tns:IWcfFoodAndAllergi_GetRecipeByType_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByTypeResponse" message="tns:IWcfFoodAndAllergi_GetRecipeByType_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="GetRecipeByName">
<wsdl:input wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByName" message="tns:IWcfFoodAndAllergi_GetRecipeByName_InputMessage"/>
<wsdl:output wsaw:Action="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByNameResponse" message="tns:IWcfFoodAndAllergi_GetRecipeByName_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IWcfFoodAndAllergi" type="tns:IWcfFoodAndAllergi">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="InsertUser">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/InsertUser" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetUser">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/GetUser" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="InsertRecipe">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/InsertRecipe" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetRecipeByAllergi">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByAllergi" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetRecipeByType">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByType" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetRecipeByName">
<soap:operation soapAction="http://nordahl.org/IWcfFoodAndAllergi/GetRecipeByName" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WcfFoodAndAllergi">
<wsdl:port name="BasicHttpBinding_IWcfFoodAndAllergi" binding="tns:BasicHttpBinding_IWcfFoodAndAllergi">
<soap:address location="http://192.168.0.18/WcfFoodAndAllergi.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

I know that its a wall of text, Im just completely lost at this point.

Any pointers would be greatly appreciated

来源:https://stackoverflow.com/questions/23690566/getting-a-nullreference-when-passing-a-complex-object-from-ksoap2-to-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!