1

这是 .net 中定义的 User 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Properties
{

    public class User
    {
        private int userId;
        private String location;
        private String firstName;
        private String lastName;
        private String email;
        private String password;
        private int userType;
        private int loginStatus;
        private String login;


        public int UserId
        {
            get
            {
                return userId;
            }

            set
            {
                userId = value;
            }

        }

        public String Location
        {
            get
            {
                return location;
            }

            set
            {
                location = value;
            }

        }

        public String FirstName
        {
            get
            {
                return firstName;
            }

            set
            {
                firstName = value;
            }

        }

        public String LastName
        {
            get
            {
                return lastName;
            }

            set
            {
                lastName = value;
            }
        }

        public String Email
        {
            get
            {
                return email;
            }

            set
            {
                email = value;
            }
        }

        public String Password
        {
            get
            {
                return password;
            }

            set
            {
                password = value;
            }
        }


        public int UserType
        {
            get
            {
                return userType;
            }

            set
            {
                userType = value;
            }

        }

        public int LoginStatus
        {
            get
            {
                return loginStatus;
            }

            set
            {
                loginStatus = value;
            }

        }


        public String Login
        {
            get
            {
                return login;
            }

            set
            {
                login = value;
            }

        }
    }

}

这是.net中Web服务的方法签名

 [OperationContract]
        int addUser(User user);

此方法将用户信息添加到数据库。

我想在android中调用这个网络服务方法

这是用户类定义

package com.webservice;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class User implements KvmSerializable {

    public int userId;
    public String location;
    public String firstName;
    public String lastName;
    public String email;
    public String password;
    public int userType;
    public int loginStatus;
    public String login;

    public User() {
    }

    public User(int userId, String location, String firstName, String lastName,
            String email, String password, int userType, int loginStatus,
            String login) {
        this.userId = userId;
        this.location = location;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.userType = userType;
        this.loginStatus = loginStatus;
        this.login = login;

    }

    public Object getProperty(int arg0) {
        switch (arg0) {
        case 0:
            return userId;
        case 1:
            return location;
        case 2:
            return firstName;
        case 3:
            return lastName;
        case 4:
            return email;
        case 5:
            return password;
        case 6:
            return userType;
        case 7:
            return loginStatus;
        case 8:
            return login;
        }

        return null;
    }

    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 9;
    }

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch (index) {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "userId";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "location";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "firstName";
            break;
        case 3:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "lastName";
            break;
        case 4:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "email";
            break;
        case 5:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "password";
            break;
        case 6:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "userType";
            break;
        case 7:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "login";
            break;
        default:
            break;
        }
    }

    public void setProperty(int index, Object value) {
        switch (index) {
        case 0:
            userId = Integer.parseInt(value.toString());
            break;
        case 1:
            firstName = value.toString();
            break;
        case 2:
            lastName = value.toString();
            break;
        case 3:
            email = value.toString();
            break;
        case 4:
            password = value.toString();
            break;
        case 5:
            userType = Integer.parseInt(value.toString());
            break;
        case 6:
            loginStatus = Integer.parseInt(value.toString());
            break;
        case 7:
            login = value.toString();
            break;
        default:
            break;
        }
    }

}

这是活动代码

public class WebServiceDemoActivity extends Activity {
    /** Called when the activity is first created. */

    private static String SOAP_ACTION3 = "http://tempuri.org/IServiceLibrary/addUser";
    private static String NAMESPACE = "http://tempuri.org/";    
    private static String METHOD_NAME3 = "addUser";
    private static String URL = "http://xxx.xxx.xxx.xxx:8030/ServiceLibrary/Service1.svc?wsdl";

    Button btnFar, btnCel, btnClear;
    EditText txtFar, txtCel;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnFar = (Button) findViewById(R.id.btnFar);        

        btnFar.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                    long messageId = 0;
                    Boolean messageSpecified;
                    User user = new User();
                    user.email = "arunm@camo.com";
                    user.location = "banaglore";
                    user.firstName = "arun";
                    user.lastName = "m";
                    user.login = "arun";
                    user.password = "camo@india";
                    user.userType = 0;
                    user.loginStatus = 1;


                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME3); 
                    PropertyInfo propertyInfo = new PropertyInfo();
                    propertyInfo.setName("user");
                    propertyInfo.setValue(user);
                    propertyInfo.setType(user.getClass());
                    request.addProperty(propertyInfo);


                    SoapSerializationEnvelope envelope = 
                        new SoapSerializationEnvelope(SoapEnvelope.VER11); 
                    envelope.dotNet=true;
                    envelope.setOutputSoapObject(request);
                    envelope.addMapping(NAMESPACE, "User", user.getClass(), new Marshal(

                    ) {

                        public void writeInstance(XmlSerializer arg0, Object arg1)
                                throws IOException {
                            // TODO Auto-generated method stub

                        }

                        public void register(SoapSerializationEnvelope arg0) {
                            // TODO Auto-generated method stub

                        }

                        public Object readInstance(XmlPullParser arg0, String arg1, String arg2,
                                PropertyInfo arg3) throws IOException, XmlPullParserException {
                            // TODO Auto-generated method stub
                            return null;
                        }
                    });
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    SoapFault soapFault=null;
                    SoapObject soapObject=null;
                    try {
                        androidHttpTransport.call(SOAP_ACTION3, envelope);
                        Object body = envelope.bodyIn;
                        if(body instanceof SoapObject)
                            soapObject = (SoapObject) envelope.bodyIn;
                        else if(body instanceof SoapFault){
                            soapFault = (SoapFault)envelope.bodyIn;
                            System.out.println("Error encountered is:"+soapFault.faultstring);
                            //Toast.makeText(getApplicationContext(), "", duration)
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("Error encountered is:"+e.getMessage());
                    }

                    if (messageId == -1) {
                        Toast.makeText (getApplicationContext(), "Failed to send message", Toast.LENGTH_SHORT);
                    } else {
                        Toast.makeText (getApplicationContext(), "Message sent successfully, message id is:"+messageId, Toast.LENGTH_SHORT);
                    }               
            }
        });
    }
}

当我跑步时,我遇到了错误

SoapFault - faultcode: 'a:DeserializationFailed' faultstring: '格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:user时出错。InnerException 消息是“第 1 行位置 344 中的错误。元素“http://tempuri.org/:user”包含映射到名称“http://tempuri.org/:User”的类型的数据。反序列化器不知道映射到此名称的任何类型。考虑使用 DataContractResolver 或将与“用户”对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表中。有关更多详细信息,请参阅 InnerException。故障者:“空”

请帮帮我,很紧急。

提前致谢。

4

0 回答 0