2013-04-09 61 views
0

所以,我是移動開發和互操作性的新手。我正在開發一個Android應用程序,comunnicates將一個Contact對象發送給運行.NET Web服務的移除服務器(通過kSOAP)。但是服務器有500錯誤,我真的不知道如何映射它,因爲它是遠程運行的,而且我的主機計劃不允許我遠程調試。我相信這是對ASP.NET Web服務中的Java對象的解釋中的錯誤,我不知道是否有必要反序列化對象以及如何執行它。我希望有經驗的開發人員能幫助我。我已經使用原始對象(字符串,int和其他),它的工作就像一個魅力,但我真的更喜歡與我的模型。如何使用kSOAP從Android發送/接收自定義對象到.NET?

的Android應用程序 - 調用Web服務的:在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; 
    } 
} 
} 

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

自定義對象。在C#.Net網絡服務

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

自定義對象:

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

+0

你有沒有找到答案? – meanbunny 2013-10-06 17:51:49

回答

-1

ThiagoMello,我幾天前有這個錯誤並解決它從這個站點generting java類:

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

有了這個班我的代碼工作正常!嘗試一下。

希望這會有所幫助。

瓊。

+0

非常感謝,但我已經放棄了這個項目。但它可能會在未來再次幫助我! Okey! – ThiagoMello 2014-02-21 14:12:36

+0

Okey!我是這麼想的,但是我決定回答,以防將來某個人出現同樣的錯誤......祝你好運! – jcasadellaoller 2014-02-21 15:55:53