4

我开发了一个 android 应用程序,我尝试调用 Web 服务并将类对象作为参数传递给方法。该类有一个名为 Partner 的列表字段,我认为该字段从未发送到 Web 服务。

这是我的客户类:

package com.testcustomer22;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

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

public class Customer extends MainActivity implements KvmSerializable
{

private static final long serialVersionUID = 1L;

public String Code;
public String Name;
public String VATNo;
public String PriceList;
public List<Partner> Partner = new ArrayList<Partner>();


    public Customer(){}


    public Customer(String cCode, String cName, String cVATNo, String cPriceList,    List<Partner> cPartner) {

    Code = cCode;
    Name = cName;
    VATNo = cVATNo;
    PriceList = cPriceList;
    Partner = cPartner;
}

public Object getProperty(int index) {
    Object res = null;
    switch(index)
    {
      case 0:
        res = this.Code;
        break;
  case 1:
        res = this.Name;
        break;
  case 2:
    res = this.VATNo;
        break;
  case 3:
        res = this.PriceList;
    break;
      case 4:
    res = this.Partner;
    break;
    }
    return res;
}

public int getPropertyCount() {
    return 4;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch (index)
    {
    case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "Code";
        break;
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "Name";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "VATNo";
        break;
    case 3:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "PriceList";
        break; 
    case 4:
        info.type = PropertyInfo.VECTOR_CLASS;
        info.name = "Partner";
        break; 
  }
}

@SuppressWarnings("unchecked")
public void setProperty(int index, Object value) {
    switch(index)
    {
    case 0:
        this.Code = value.toString();
        break;
    case 1:
        this.Name = value.toString();
        break;
case 2:
        this.VATNo = value.toString();
        break;
    case 3:
    this.PriceList = value.toString();
        break;
case 4:
        this.Partner = (List<com.testcustomer22.Partner>) value;
        break;
default:
        break;
    }
  }
}

这是我调用网络服务的 MainActivity:

package com.testcustomer22;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity 
{    

    private static String SOAP_ACTION1 = "http://tempuri.org/Service/GetCustomer";
    private static String NAMESPACE = "http://tempuri.org/";
    private static String METHOD_NAME1 = "GetCustomer";
    private static String URL = "http://xxxxxxxx:8000/Service?wsdl";


    TextView TextView01;
    EditText editText2,editText1;
    Button button1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

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

        button1.setOnClickListener(new View.OnClickListener()
            {
            @Override 
               public void onClick(View v)
               { 

            TextView01 = (TextView)findViewById(R.id.TextView01);
                editText2 = (EditText)findViewById(R.id.editText2);
                editText1 = (EditText)findViewById(R.id.editText1);

                Customer C = new Customer();
                C.setProperty(0,"0000000000000000");

                    //Initialize soap request + add parameters
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

                    //Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

                //Pass value for userName variable of the web service
                PropertyInfo pi =new PropertyInfo(); 
                pi.setName("customer");    //Define the variable name
                pi.setValue(C);            //set value for userName variable
                pi.setType(C.getClass());  //Define the type of the variable
                request.addProperty(pi);   //Pass properties to the variable 


                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.debug = true;
                envelope.dotNet = true;     


                envelope.addMapping(NAMESPACE, "customer", new Customer().getClass());


            try{

                    editText2.setText("before call websrv");

                        //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION1, envelope);

                        //Get the SoapResult from the envelope body.
                SoapObject response = (SoapObject) envelope.bodyIn;

                    Log.i("REQUEST--->", androidHttpTransport.requestDump); 
                    Log.i("RESPONSE--->", androidHttpTransport.responseDump); 

                    Log.i("Response.toString()---------->", response.toString()); 

                }catch (Exception e) 
                {
                    e.printStackTrace();
                }
             }
        });
     }
 }

我添加了“Log.i(”REQUEST--->“...”这一行来打印发送给服务的请求。问题是当我将“return 5”写入“getPropertyCount()”时我的客户类的方法并尝试运行我的主要活动它把我抛出异常。当我将“return4”写入“getPropertyCount()”时,它返回给我:

I/REQUEST---------------------->(1156): <v:Envelope 
xmlns:i="http://www.w3.org/1999/XMLSchema-instance" 
xmlns:d="http://www.w3.org/1999/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><GetCustomer 
xmlns="http://tempuri.org/" id="o0" c:root="1"><customer i:type="d:anyType"><Code 
i:type="d:string">0000000000000000</Code><Name i:null="true" /><VATNo i:null="true" />
<PriceList i:null="true" /></customer></GetCustomer></v:Body></v:Envelope>

它显示除合作伙伴之外的所有其他字段。

如果您对如何正确传递列表字段有任何想法,请帮助我。

非常感谢。

4

0 回答 0