2014-02-24 27 views
0

我有使用kso​​ap2向.net服務發送自定義對象的問題。我得到這個錯誤:服務器無法處理請求將自定義對象從android發送到.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> 

在安卓這個我自定義類

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; } 
    } 

這是我服務的asm​​x

[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(); 
     } 

SOAP請求這個樣子的

<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:服務器無法處理請求。 System.NullReferenceException:未將對象引用設置爲對象的實例。

任何幫助將不勝感激:)

回答

0

看起來像一個命名空間問題給我。根據WSDL,Web服務期望 元素位於http://tempuri.org/encodedTypes名稱空間中,但在請求中使用的名稱空間爲http://tempuri.org/

+1

我已經試過了。我把'targetNamespace =「http://tempuri.org/encodedTypes」'映射部分不在發送請求部分,因爲我看到了這樣的例子。 – mcorluka

相關問題