簡體   English   中英

將類作為參數傳遞給SOAP Web服務

[英]Passing a class to the SOAP web service as parameter in Java

嗨,我想將類作為參數傳遞給Web服務。 這是我的網絡服務代碼:

<xs:complexType name="RegisterStudent">
<xs:sequence>
<xs:element name="student" type="tns:student" minOccurs="0"/>
<xs:element name="user" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

這是我的調用函數:

public static String registerStudentClass(String user) {
soapAction="http://auth.ws.df.com/RegisterStudent";
methodName="RegisterStudent";
String resTxt = null;

Student student= new Student();
student.setAge(22);
student.setName("Jerry");

SoapObject request = new SoapObject(NAMESPACE, methodName);
request.addProperty("student", student);//Student class added here
request.addProperty("user", user);//User name, passed as string

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
System.out.println("androidHttpTransport envelope");

    try{
        androidHttpTransport.call(soapAction, envelope);
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        resTxt = response.toString();
    }catch(Exception e){
        e.printStackTrace();
        resTxt = "Error occured\n"+e;
    }

    return resTxt;

}

這是我的學生班:

Public Class Student implements Serializable{
  private int age;
  private String name;
  public Student(){}
  public Student(int age, String name){
    this.age = age;
    this.name = name;
  }
  //setter and getter methods come here.
  //...
}

運行此命令后,出現以下錯誤:“無法序列化:Student @ 4329d250”。請提供幫助。

提前致謝

因此,我想出了解決方法:首先,使類可序列化

Public Class Student implements KvmSerializable{

private int age;
private String name;

public Student(){}

public Student(int age, String name){
   this.age = age;
   this.name = name;
  }
  //setter and getter methods come here.
  //...

  public Object getProperty(int arg0) {
      switch(arg0){
      case 0: 
          return getAge();
      case 1:
          return getName();
      }
      return null;
  }

  public int getPropertyCount() {
      return 2;
  }

  public void setProperty(int index, Object value) {
      switch(index){
      case 0 :
         age = value.toString();
         break;
      case 1 :
         name = value.toString();
         break;
      default:
         break;
      }
  }
}

WebService方法如下所示:

public static String registerStudentClass(Student stud) {
  soapAction="http://auth.ws.df.com/RegisterStudent";
  methodName="RegisterStudent";
  String resTxt = null;

  //new change
  PropertyInfo pi = new PropertyInfo();
  pi.setName("stud");
  pi.setValue(stud);
  pi.setType(stud.getClass());

  SoapObject request = new SoapObject(NAMESPACE, methodName);
  request.addProperty(pi);

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
  SoapEnvelope.VER11);
  envelope.setOutputSoapObject(request);//new change
  envelope.addMapping(NAMESPACE, "Student", new Student().getClass());
  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  System.out.println("androidHttpTransport envelope");

  try{
      androidHttpTransport.call(soapAction, envelope);
      SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
      resTxt = response.toString();
   }catch(Exception e){
      e.printStackTrace();
      resTxt = "Error occured\n"+e;
  }

  return resTxt;
}

我假設響應是文本,例如1-注冊成功2-注冊失敗等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM