简体   繁体   English

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

[英]How to send/receive a custom object from Android to .NET using kSOAP?

So, I am a newbie in mobile development and interoperability. 因此,我是移动开发和互操作性的新手。 I am developing a Android application that comunnicates send a Contact object to a remove server that is running a .NET Web Service (via kSOAP). 我正在开发一个用于通信的Android应用程序,用于将Contact对象发送到运行.NET Web服务(通过kSOAP)的删除服务器。 But there is a 500 error in the server and I really don't know how to map it because it is running remotely and my host plan doesn't allow me to debug remotely. 但是服务器中有500个错误,我真的不知道如何映射它,因为它正在远程运行,并且我的主机计划不允许我进行远程调试。 I believe it is an error in the interpretation of the Java object in the ASP.NET Web Service, I don't know if it is necessary to deserialize the object and how to do it. 我相信在ASP.NET Web服务中解释Java对象时出错,我不知道是否有必要对对象进行反序列化以及如何对其进行反序列化。 I hope that a more experienced developer could help me. 我希望有经验的开发人员可以为我提供帮助。 I already used primitive objects (string, int and others) and it worked like a charm but I really prefer to work with my models. 我已经使用了原始对象(字符串,整数和其他对象),它的工作原理很吸引人,但是我真的更喜欢使用模型。

The Android Application - Call of the Web Service: 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());
    }
}

Custom Object in Java 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 Service 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;
        }     
    }
}
}

The Custom Object in C#: 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;
        }
    }
}
}

The SOAP Request XML: 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>

I've been stuck in this for a while and didn't found any implementation and deserialization of a java model in .NET. 我被困在这里已经有一段时间了,在.NET中没有发现Java模型的任何实现和反序列化。 I hope someone out there helps me! 我希望那里的人能帮助我! =D = D

ThiagoMello, I had this error days ago and solve it generting java class from this site: ThiagoMello,我几天前遇到此错误,并从该站点生成java类来解决该错误:

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

With this classes my code works fine! 使用此类,我的代码可以正常工作! Try it. 试试吧。

Hope this helps. 希望这可以帮助。

Joan.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM