如何使用kSOAP从Android发送/接收自定义对象到.NET?

时间:2013-04-09 15:55:23

标签: android .net web-services deserialization ksoap

所以,我是移动开发和互操作性的新手。我正在开发一个Android应用程序,它将一个Contact对象发送到一个运行.NET Web服务的删除服务器(通过kSOAP)。但是服务器中有500个错误,我真的不知道如何映射它,因为它是远程运行的,我的主机计划不允许我远程调试。我相信在ASP.NET Web服务中解释Java对象是一个错误,我不知道是否有必要反序列化对象以及如何执行它。我希望有经验的开发人员可以帮助我。我已经使用了原始对象(字符串,整数等),它就像一个魅力,但我更喜欢使用我的模型。

Android应用程序 - 调用Web服务:

     public int webServiceMethodRegisterUser(String... paramss)
{
    String resultData;
    try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        Contact c = new Contact(paramss[0], paramss[1]);
        c._email = paramss[0];
        c._password = paramss[1];
        c._cellphonesim = paramss[2];
        c._simoperator = paramss[3];
        PropertyInfo pi = new PropertyInfo();
        pi.setName("contact");
        pi.setValue(c);
        pi.setType(c.getClass());
        request.addProperty(pi);
        //request.addProperty("useremail", paramss[0]);
        //request.addProperty("userpass", paramss[1]);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        envelope.addMapping(NAMESPACE, "Contact" , new Contact().getClass());
        //Marshal floatMarshal = new Marshal();
        //floatMarshal.register(envelope);
        //Marshal oi = new ();

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Object result=(Object)envelope.getResponse();
      //To get the data.
        resultData= result.toString();
        publishProgress(resultData);
        return Integer.parseInt(resultData);
    } catch (Exception e) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {  
            throw new RuntimeException(e1.toString());

          }  
        throw new RuntimeException(e.toString());
    }
}

Java中的自定义对象

package com.example.ivi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

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

 public class Contact implements KvmSerializable {

// private variables
int _id;
String _email;
String _password;
String _simoperator;
String _cellphonesim;
String _sn_facebook;
String _sn_linkedin;
String _sn_googleplus;
String _sn_orkut;
String _sn_twitter;
Date _last_refresh;
Date _register_date;

public Contact(int id, String email, String password, String simoperator,
        String cellphonesim, String facebook, String linkedin,
        String googleplus, String orkut, String twitter, Date refreshuser,
        Date userregister) {
    _id = id;
    _email = email;
    _password = password;
    _simoperator = simoperator;
    _cellphonesim = cellphonesim;
    _sn_facebook = facebook;
    _sn_linkedin = linkedin;
    _sn_googleplus = googleplus;
    _sn_orkut = orkut;
    _sn_twitter = twitter;
    _last_refresh = refreshuser;
    _register_date = userregister;
}

public Contact() {
}

// constructor
public Contact(int id, String email, String password) {
    this._id = id;
    this._email = email;
    this._password = password;
}

// constructor
public Contact(String email, String password) {
    this._email = email;
    this._password = password;
}

@Override
public Object getProperty(int arg0) {
    switch (arg0) {
    case 0:
        return _id;
    case 1:
        return _email;
    case 2:
        return _password;
    case 3:
        return _simoperator;
    case 4:
        return _cellphonesim;
    case 5:
        return _sn_facebook;
    case 6:
        return _sn_linkedin;
    case 7:
        return _sn_googleplus;
    case 8:
        return _sn_orkut;
    case 9:
        return _sn_twitter;
    case 10:
        return _last_refresh;
    case 11:
        return _register_date;
    }
    return null;
}

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

@Override
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 = "UserEmail";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserPassword";
        break;
    case 3:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserSimOperator";
        break;
    case 4:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserCellPhoneSim";
        break;
    case 5:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserFacebook";
        break;
    case 6:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserLinkedin";
        break;
    case 7:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserGoogleplus";
        break;
    case 8:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserOrkut";
        break;
    case 9:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "UserTwitter";
        break;
    case 10:
        info.type = MarshalDate.DATE_CLASS;
        ;
        info.name = "UserLastRefresh";
        break;
    case 11:
        info.type = MarshalDate.DATE_CLASS;
        ;
        info.name = "UserRegisterDate";
        break;
    }
}

@Override
public void setProperty(int index, Object value) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    switch (index) {
    case 0:
        _id = Integer.parseInt(value.toString());
        break;
    case 1:
        _email = value.toString();
        break;
    case 2:
        _password = value.toString();
        break;
    case 3:
        _simoperator = value.toString();
        break;
    case 4:
        _cellphonesim = value.toString();
        break;
    case 5:
        _sn_facebook = value.toString();
        break;
    case 6:
        _sn_linkedin = value.toString();
        break;
    case 7:
        _sn_googleplus = value.toString();
        break;
    case 8:
        _sn_orkut = value.toString();
        break;
    case 9:
        _sn_twitter = value.toString();
        break;
    case 10:
        try {
            _last_refresh = formatter.parse(value.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    case 11:
        try {
            _register_date = formatter.parse(value.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
}
 }

。 NET Web服务

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

 namespace WebServiceIvi
{

[WebService(Namespace = "http://www.ividomain.somee.com/Service1")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]

public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public int registerUser(Contact contato)
    {
        string connString = CONNECTION_STRING;
        SqlConnection con = new SqlConnection(connString);
        SqlCommand cmdSQL = con.CreateCommand();
        try
        {
            con.Open();
            cmdSQL.CommandText = String.Format("INSERT INTO [databaseivi].[dbo].[UsersIvi]  ([Useremail],[Userpass],[Userxml],[Usersince]) VALUES('{0}','{1}',null ,getDate())", contato.UserEmail, contato.UserPassword);
            int result = cmdSQL.ExecuteNonQuery();
            con.Close();

            return result;
        }
        catch (SqlException e)
        {
            foreach (SqlError error in e.Errors)
            {
                return error.Number;
            }
            return 0;
        }     
    }
}
}

C#中的自定义对象:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace WebServiceIvi
{
public class Contact
{
    public Contact(int id, String email, String password, String simoperator, String     cellphonesim, String facebook, String linkedin, String googleplus, String orkut, String twitter, DateTime refreshuser, DateTime userregister )
    {
        _id = id;
         _email = email;
         _password = password;
         _simoperator = simoperator;
         _cellphonesim = cellphonesim;
         _sn_facebook = facebook;
         _sn_linkedin = linkedin;
         _sn_googleplus = googleplus;
         _sn_orkut = orkut;
         _sn_twitter = twitter;
        _last_refresh = refreshuser;
        _register_date = userregister;
    }

    public Contact()
    {
    }

    int _id;
    String _email;
    String _password;
    String _simoperator;
    String _cellphonesim;
    String _sn_facebook;
    String _sn_linkedin;
    String _sn_googleplus;
    String _sn_orkut;
    String _sn_twitter;
    DateTime _last_refresh;
    DateTime _register_date;

    public int UserId
    {
        set { _id = value; }
        get
        {
            if (_id <= 0) return _id;
            else return 0;
        }
    }
    public string UserEmail
    {
        set { _email = value; }
        get
        {
            if (_email != null) return _email.ToString();
            else return string.Empty;
        }
    }
    public string UserPassword
    {
        set { _password = value; }
        get
        {
            if (_password != null) return _password.ToString();
            else return string.Empty;
        }
    }
    public string UserSimOperator
    {
        set { _simoperator = value; }
        get
        {
            if (_simoperator != null) return _simoperator.ToString();
            else return string.Empty;
        }
    }
    public string UserCellPhoneSim
    {
        set { _cellphonesim = value; }
        get
        {
            if (_cellphonesim != null) return _cellphonesim.ToString();
            else return string.Empty;
        }
    }
    public string UserFacebook
    {
        set { _sn_facebook = value; }
        get
        {
            if (_sn_facebook != null) return _sn_facebook.ToString();
            else return string.Empty;
        }
    }
    public string UserLinkedin
    {
        set { _sn_linkedin = value; }
        get
        {
            if (_sn_linkedin != null) return _sn_linkedin.ToString();
            else return string.Empty;
        }
    }
    public string UserGoogleplus
    {
        set { _sn_googleplus = value; }
        get
        {
            if (_sn_googleplus != null) return _sn_googleplus.ToString();
            else return string.Empty;
        }
    }
    public string UserOrkut
    {
        set { _sn_orkut = value; }
        get
        {
            if (_sn_orkut != null) return _sn_orkut.ToString();
            else return string.Empty;
        }
    }
    public string UserTwitter
    {
        set { _sn_twitter = value; }
        get
        {
            if (_sn_twitter != null) return _sn_twitter.ToString();
            else return string.Empty;
        }
    }
    public DateTime UserLastRefresh
    {
        set { _last_refresh = value; }
        get
        {
            if (_last_refresh != null) return DateTime.Parse(_last_refresh.ToString());
            else return DateTime.MinValue;
        }
    }
    public DateTime UserRegisterDate
    {
        set { _register_date = value; }
        get
        {
            if (_register_date != null) return DateTime.Parse(_register_date.ToString());
            else return DateTime.MinValue;
        }
    }
}
}

SOAP请求XML:

  POST /Service1.asmx HTTP/1.1
  Host: localhost
  Content-Type: text/xml; charset=utf-8
  Content-Length: length
  SOAPAction: "http://www.ividomain.somee.com/Service1/registerUser"

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
   <registerUser xmlns="http://www.ividomain.somee.com/Service1">
    <contato>
    <UserId>int</UserId>
    <UserEmail>string</UserEmail>
    <UserPassword>string</UserPassword>
    <UserSimOperator>string</UserSimOperator>
    <UserCellPhoneSim>string</UserCellPhoneSim>
    <UserFacebook>string</UserFacebook>
    <UserLinkedin>string</UserLinkedin>
    <UserGoogleplus>string</UserGoogleplus>
    <UserOrkut>string</UserOrkut>
    <UserTwitter>string</UserTwitter>
    <UserLastRefresh>dateTime</UserLastRefresh>
    <UserRegisterDate>dateTime</UserRegisterDate>
  </contato>
 </registerUser>
 </soap:Body>
 </soap:Envelope>

我已经坚持了一段时间,并没有在.NET中找到任何java模型的实现和反序列化。我希望那里有人帮助我! = d

1 个答案:

答案 0 :(得分:-1)

ThiagoMello,我在几天前遇到了这个错误并解决了从这个网站创建java类的问题:

http://www.wsdl2code.com/Pages/Home.aspx

使用这些类,我的代码工作正常!试试吧。

希望这有帮助。

琼。