2

我正在尝试使用 eclipse 创建一个 android 应用程序,ksoap并且我想发送一个类对象作为 Web 服务方法中的参数,并将列表作为响应。我找到了这些链接1 链接2 ,但我没有设法编写有效的代码。

这是我的代码:

package com.testcustomer11;

import org.ksoap2.SoapEnvelope;
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.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/GetCustomer";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "GetCustomer";
private static String URL = "http://localhost:8000/Service?wsdl";

Button button1;
EditText editText2;
TextView TextView01;

@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)
        {
        editText2 = (EditText)findViewById(R.id.editText2);

        TextView01 = (TextView)findViewById(R.id.TextView01);

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


        Customer C = new Customer();



        request.addProperty("customer",editText2.getTag());

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


        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.debug = true;

        try {
            //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;

            SoapObject soapResult = (SoapObject)response.getProperty(0);

            for (int i = 0; i < soapResult.getPropertyCount(); i++) {
            SoapObject res = (SoapObject) response.getProperty(i);
                C.Code = res.getProperty(0).toString();
                C.Name = res.getProperty(1).toString();
                C.VATNo = res.getProperty(2).toString();
                C.PriceList = res.getProperty(3).toString();
                SoapObject so = (SoapObject) soapResult.getProperty(i);
                so.getProperty("Code");

            }
            TextView01.setText(response.toString());
        }
        catch (Exception e) 
        {
            //TextView01.setText("EXCEPTION");
            e.printStackTrace();
        }
        }
        });    
        }
        }

和客户类:

package com.testcustomer11;

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Customer extends MainActivity implements KvmSerializable
{
public int Code;
public String Name;
public String VATNo;
public String PriceList;

    public Customer(){}

public Customer(int cCode, String cName, String cVATNo, String cPriceList) {

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

public Object getProperty(int arg0) {

    switch(arg0)
    {
    case 0:
        return Code;
    case 1:
        return Name;
    case 2:
        return VATNo;
    case 3:
        return PriceList;
    }
    return null;
}

public int getPropertyCount() {
    return 3;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case 0:
        info.type = PropertyInfo.INTEGER_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;
    default:break;
    }
}

public void setProperty(int index, Object value) {
    switch(index)
    {
    case 0:
        Code = Integer.parseInt(value.toString());
        break;
    case 1:
        Name = value.toString();
        break;
    case 2:
        VATNo = value.toString();
        break;
    case 3:
        PriceList = value.toString();
        break;
    default:
        break;
    }
   }
 }

当我在模拟器中运行它时会给我这个消息"GetCustomerResponse{GetCustomerResult=anyType{};}"

有任何想法吗??

4

0 回答 0