【问题标题】:I am facing an issue in formatting a complex soap object in android我在格式化android中的复杂soap对象时遇到问题
【发布时间】:2015-01-03 10:49:26
【问题描述】:

如果有人有使用android 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- 登录请求类

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

【问题讨论】:

    标签: android web-services wcf ksoap2 android-ksoap2


    【解决方案1】:

    我试图尽可能靠近你的信封,但这有点棘手。我所做的是标签上的正确结构和名称空间 uri,试试这个 - 也许 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、UserName 和 Password 添加了命名空间:

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

    问候 马辛

    ps。向信封对象添加了implicitTypes=true。这会删除 i:type 属性 - 尤其是 i:type="n0:request"。

    【讨论】:

    • 将implicitTypes=true 添加到信封对象。这将删除 i:type 属性 - 尤其是 i:type="n0:request"。如果不起作用,请立即尝试。
    猜你喜欢
    • 2022-06-25
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2013-03-03
    • 2022-01-03
    相关资源
    最近更新 更多