将自定义对象从android发送到.Net服务

时间:2014-02-24 11:53:35

标签: android .net web-services wcf ksoap2

我在使用kso​​ap2向.net服务发送自定义对象时遇到问题。我收到此错误:服务器无法处理请求

这是我的WSDL

<wsdl:definitions xmlns:s0="http://tempuri.org/encodedTypes" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/encodedTypes">
<s:complexType name="Recept">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="ID_TRETMAN" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="ID_LIJEK" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="VRIJEME" type="s:dateTime"/>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="PUTPRIM" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="KOLICINA" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="RECEPT" type="s:boolean"/>
</s:sequence>
</s:complexType>
<s:complexType name="Test">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" form="unqualified" name="ID" type="s:int"/>
<s:element minOccurs="0" maxOccurs="1" form="unqualified" name="NAME" type="s:string"/>
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="setReceptSoapIn">
<wsdl:part name="r" type="s0:Recept"/>
</wsdl:message>
<wsdl:message name="setReceptSoapOut">
<wsdl:part name="setReceptResult" type="s0:Recept"/>
</wsdl:message>
<wsdl:message name="setTestSoapIn">
<wsdl:part name="t" type="s0:Test"/>
</wsdl:message>
<wsdl:message name="setTestSoapOut">
<wsdl:part name="setTestResult" type="s0:Test"/>
</wsdl:message>
<wsdl:message name="setNestoSoapIn">
<wsdl:part name="i" type="s:int"/>
</wsdl:message>
<wsdl:message name="setNestoSoapOut">
<wsdl:part name="setNestoResult" type="s:int"/>
</wsdl:message>
<wsdl:portType name="NEADSwebServiceSoap">
<wsdl:operation name="setRecept">
<wsdl:input message="tns:setReceptSoapIn"/>
<wsdl:output message="tns:setReceptSoapOut"/>
</wsdl:operation>
<wsdl:operation name="setTest">
<wsdl:input message="tns:setTestSoapIn"/>
<wsdl:output message="tns:setTestSoapOut"/>
</wsdl:operation>
<wsdl:operation name="setNesto">
<wsdl:input message="tns:setNestoSoapIn"/>
<wsdl:output message="tns:setNestoSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="NEADSwebServiceSoap" type="tns:NEADSwebServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="setRecept">
<soap:operation soapAction="http://tempuri.org/setRecept" style="rpc"/>
<wsdl:input>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="setTest">
<soap:operation soapAction="http://tempuri.org/setTest" style="rpc"/>
<wsdl:input>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="setNesto">
<soap:operation soapAction="http://tempuri.org/setNesto" style="rpc"/>
<wsdl:input>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="NEADSwebServiceSoap12" type="tns:NEADSwebServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="setRecept">
<soap12:operation soapAction="http://tempuri.org/setRecept" style="rpc"/>
<wsdl:input>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="setTest">
<soap12:operation soapAction="http://tempuri.org/setTest" style="rpc"/>
<wsdl:input>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="setNesto">
<soap12:operation soapAction="http://tempuri.org/setNesto" style="rpc"/>
<wsdl:input>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="encoded" namespace="http://tempuri.org/" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="NEADSwebService">
<wsdl:port name="NEADSwebServiceSoap" binding="tns:NEADSwebServiceSoap">
<soap:address location="http://192.168.111.207:8080/AndroidServices/NEADSWebService01/NEADSwebService.asmx"/>
</wsdl:port>
<wsdl:port name="NEADSwebServiceSoap12" binding="tns:NEADSwebServiceSoap12">
<soap12:address location="http://192.168.111.207:8080/AndroidServices/NEADSWebService01/NEADSwebService.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这是Android中的自定义类

public class Test implements KvmSerializable{

    public Test()
    {

    }

    public int ID;
    public String NAME;

    @Override
    public Object getProperty(int index) {
        switch(index)
        {
        case 0:
            return ID;
        case 1:
            return NAME;
        }

        return null;
    }
    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 2;
    }
    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index)
        {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ID";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "NAME";
            break;
        }

    }
    @Override
    public void setProperty(int index, Object value) {
        switch(index)
        {
            case 0:
                ID = Integer.parseInt(value.toString());
            break;
            case 1:
                NAME = value.toString();
            break;
        }

    }   

}

这是我在.net服务中的映射类

 public class Test
    {

        public Test()
        {}

        public int ID {get; set;}

        public String NAME { get; set; }
    }

这是我的服务asmx

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class NEADSwebService : System.Web.Services.WebService
    {

        public NEADSwebService()
        { }

        [SoapRpcMethod]
        [WebMethod]
        public Recept setRecept(Recept r)
        {
            return r;
        }

       [SoapRpcMethod]
       [WebMethod]
       public Test setTest(Test t)
       {
           using (var context = new EntitiyFramework.AndroidGPSEntities())
           { 
               context.AddToGPSCoordinates(new GPSCoordinate(){ x = t.ID, xString = t.NAME, Time = DateTime.Now});
               context.SaveChanges();
           }
           return t;
       }

       [SoapRpcMethod]
       [WebMethod]
       public int setNesto(int i)
       {
           int a = 0;
           using (var context = new EntitiyFramework.AndroidGPSEntities())
           {
               context.AddToGPSCoordinates(new GPSCoordinate() { x = i, xString = "dsd", Time = DateTime.Now });
               context.SaveChanges();
               a = 5;
           }
           return a;
       }
    }
}

这是我发送服务请求的代码

private static final String SOAP_ACTION = "http://tempuri.org/setTest";     
    private static final String METHOD_NAME = "setTest";     
    private static final String NAMESPACE = "http://tempuri.org/";         
    private static final String URL = "http://192.168.111.207:8080/AndroidServices/NEADSWebService01/NEADSwebService.asmx"; 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
         PropertyInfo pi = new PropertyInfo();             

         pi.setName("t"); 
         Test t = new Test();
         t.ID = 12;
         t.NAME = "Ante";
         pi.setValue(t);
         pi.setType(Test.class);
         request.addProperty(pi);

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);             
         envelope.dotNet = true;    

         envelope.setOutputSoapObject(request); 
         envelope.addMapping("http://tempuri.org/encodedTypes", "Test", new Test().getClass()); 

         HttpTransportSE httpTransport = new HttpTransportSE(URL);
         httpTransport.debug =true;
         try {
            httpTransport.call(SOAP_ACTION, envelope);
            Log.d("SERVICE request", httpTransport.requestDump);
            Log.d("SERVICE response", httpTransport.responseDump);
            Vector response = (Vector)envelope.getResponse();
            Log.d("SERVICE", "Uspio");
            //Log.d("SERVICE", response.get(1).toString());

         } catch (HttpResponseException e) {
             Log.d("SERVICE", "GRESKA 1");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("SERVICE", "GRESKA 2");
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("SERVICE", "GRESKA 3");
            e.printStackTrace();
        } 

肥皂请求看起来像这样

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body><setTest xmlns="http://tempuri.org/" id="o0" c:root="1">
<t i:type="n0:Test" xmlns:n0="http://tempuri.org/">
<ID i:type="d:int">12</ID>
<NAME i:type="d:string">Ante</NAME>
</t>
</setTest>
</v:Body>
</v:Envelope>

每次我发送请求时都会收到同样的错误:SoapException:Server无法处理请求。 System.NullReferenceException:未将对象引用设置为对象的实例。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

对我来说看起来像名称空间问题。根据WSDL,Web服务期望 要在http://tempuri.org/encodedTypes命名空间中的元素,但在请求中使用命名空间http://tempuri.org/