2015-01-03 5 views
0

Пожалуйста, если кто-нибудь есть опыт в потребляющей службе WCF от андроидая столкнулся вопрос при форматировании сложного объекта мыл в андроиде

формата, который мне нужен

<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:tem="http://tempuri.org/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://schemas.datacontract.org/2004/07/WebService.Contracts"> 
    <v:Header/> 
    <v:Body> 
     <tem:Login> 
     <tem:request> 
      <web:UserID>0</web:UserID> 
      <web:Password>1</web:Password> 
      <web:UserName>admin</web:UserName> 
     </tem:request> 
     </tem:Login> 
    </v:Body> 
</v:Envelope> 

Формата порождена моего кода

<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> 
    <Login xmlns="http://tempuri.org/" id="o0" c:root="1"> 
     <UserID i:type="d:string">0</UserID> 
     <UserName i:type="d:string">admin</UserName> 
     <Password i:type="d:string">1</Password> 
    </Login> 
</v:Body> 

Мой код следующий

1- LoginRequest Класс

public class LoginRequest extends SoapObject implements KvmSerializable { 

    private String mUserID; 
    private String mUserName; 
    private String mPassword; 

    public LoginRequest() { 

     super(AppConstants.NAMESPACE, AppConstants.METHOD_NAME_LOGIN); 
    } 

    @Override 
    public Object getProperty(int index) { 

     switch (index) { 
     case 0: 
      return mUserID; 
     case 1: 
      return mUserName; 
     case 2: 
      return mPassword; 
     default: 
      break; 
     } 
     return null; 
    } 

    @Override 
    public int getPropertyCount() { 

     return 3; 
    } 

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

     switch (index) { 
     case 0: 
      info.type = PropertyInfo.STRING_CLASS; 
      info.name = AppConstants.REQUEST_PARAM_USER_ID; 
      break; 
     case 1: 
      info.type = PropertyInfo.STRING_CLASS; 
      info.name = AppConstants.REQUEST_PARAM_USERNAME; 
      break; 
     case 2: 
      info.type = PropertyInfo.STRING_CLASS; 
      info.name = AppConstants.REQUEST_PARAM_PASSWORD; 
      break; 
     default: 
      break; 
     } 

    } 

    @Override 
    public void setProperty(int index, Object value) { 

     if (value == null) 
      value = ""; 

     switch (index) { 
     case 0: 
      mUserID = value.toString(); 
      break; 
     case 1: 
      mUserName = value.toString(); 
      break; 
     case 2: 
      mPassword = value.toString(); 
      break; 

     } 
    } 
} 

2- Войти Метод

try { 

         LoginRequest request = new LoginRequest(); 
         request.setProperty(0, "0"); 
         request.setProperty(1, username); 
         request.setProperty(2, password); 

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

         envelope.addMapping("http://tempuri.org/", "Login", LoginRequest.class); 

         envelope.setOutputSoapObject(request); 
         androidHttpTransport = new HttpTransportSE("my_service_url"); 
         androidHttpTransport.debug = true; 
         androidHttpTransport.call("my_service_action", "Login"), envelope); 

         if (envelope.bodyIn != null) { 

          //LoginResponse response = (LoginResponse) envelope.bodyIn; 
         } 

         System.out.println(androidHttpTransport.requestDump); 
         System.out.println(androidHttpTransport.responseDump); 

        } catch (Exception e) { 

         System.out.println(androidHttpTransport.requestDump); 
         System.out.println(androidHttpTransport.responseDump); 
         e.printStackTrace(); 
        } 

ответ

0

Я пытался получить как можно ближе к оболочке, как я могу, но это немного сложнее. Что я сделал правильно структура и пространств имен URIs тегов, попробуйте это - возможно, WS примет такой ответ:

<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> 
     <Login xmlns="http://tempuri.org/" id="o0" c:root="1"> 
      <n0:request i:type="n0:request" xmlns:n0="http://tempuri.org/"> 
       <n1:UserID i:type="d:string" xmlns:n1="http://schemas.datacontract.org/2004/07/WebService.Contracts">0</n1:UserID> 
       <n2:UserName i:type="d:string" xmlns:n2="http://schemas.datacontract.org/2004/07/WebService.Contracts">nam</n2:UserName> 
       <n3:Password i:type="d:string" xmlns:n3="http://schemas.datacontract.org/2004/07/WebService.Contracts">pas</n3:Password> 
      </n0:request> 
     </Login> 
    </v:Body> 
</v:Envelope> 

я добавил пространств имен UserID, логин и пароль таким образом:

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
    switch (index) { 
    case 0: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "UserID"; 
     info.namespace="http://schemas.datacontract.org/2004/07/WebService.Contracts"; 
     break; 
    case 1: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "UserName"; 
     info.namespace="http://schemas.datacontract.org/2004/07/WebService.Contracts"; 
     break; 
    case 2: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Password"; 
     info.namespace="http://schemas.datacontract.org/2004/07/WebService.Contracts"; 
     break; 
    default: 
     break; 
    } 
} 

и добавил внешний тег таким образом:

SoapObject request_main = new SoapObject("http://tempuri.org/", "Login"); 

LoginRequest request = new LoginRequest(); 
request.setProperty(0, "0"); 
request.setProperty(1, "nam"); 
request.setProperty(2, "pas"); 

request_main.addProperty("request", request); 

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 


envelope.dotNet = true; 
//here additional flag 
    envelope.implicitTypes = true; 

envelope.addMapping("http://tempuri.org/", "request", LoginRequest.class); 

envelope.setOutputSoapObject(request_main); 

С уважением Marcin

шт. Добавлен implicitTypes = true для объекта конверта. Это удаляет атрибуты i: type, особенно i: type = "n0: request".

+0

Добавлено implicitTypes = true для объекта конверта. Это удаляет атрибуты i: type, особенно i: type = "n0: request". Попробуйте, если это не сработает. – mmprog