使用我的Android应用程序中的soap web服务发送列表名称值对

时间:2014-04-03 05:45:37

标签: android web-services soap

我必须使用我的Android应用程序中的Soap webservice将NameValuePair列表发送到服务器。 我得到了error 04-03 11:09:51.693: E/Error in Catch(14070): java.lang.RuntimeException: Cannot serialize。请帮我。以下是我的代码

List<NameValuePair >Array = new ArrayList<NameValuePair>();
        Array .add(new BasicNameValuePair("first_name", firstName));
        Array .add(new BasicNameValuePair("last_name", lastName));
        Array .add(new BasicNameValuePair("address", address);
        Array .add(new BasicNameValuePair("city", city));
        Array .add(new BasicNameValuePair("state", state));


protected String doInBackground(List<NameValuePair>... params) {



            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("array", Array );



            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.implicitTypes = true;

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);


                SoapObject result = (SoapObject) envelope.bodyIn;
                Log.e("Soap Object Output", String.valueOf(result));


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

                Log.e("Error in Catch", e.toString());

            }

            return null;

        }

3 个答案:

答案 0 :(得分:0)

我为这个问题给了赏金,最后我找到了解决方案。 解决方案是您不能将List<NameValuePair >用于SOAP Web服务。而不是你可以使用kvmserialization。以下是您的查询的解决方案。

首先,您需要从此link

下载库

<强> Array.java

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

public class Array implements KvmSerializable {

    private String firstname;
    private String lastname;
    private String address;
    private String city;
    private String state;

    public void setFirstname(String firstname)
    {
        this.firstname=firstname;
    }

    public String getFirstname(){
        return  firstname;
    }

    public void setLastname(String lastname)
    {
        this.lastname = lastname;
    }

    public String getLast_name()
    {
        return lastname;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getAddress()
    {
        return address;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getCity()
    {
        return city;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public String getState()
    {
        return state;
    }

    @Override
    public Object getProperty(int arg0) {
        // TODO Auto-generated method stub
        switch (arg0){
        case 0:
            return firstname;
        case 1:
            return lastname;
        case 2:
            return address;
        case 3:
            return city;
        case 4:
            return state;
        default:
            return null;
            }

    }
    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 5; //number of passing parameters
    }
    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
        // TODO Auto-generated method stub
        switch(arg0)
        {

            case 0:
                arg2.type = PropertyInfo.STRING_CLASS;
                arg2.name = "first_name"; //fiels names
            break;
            case 1:
                arg2.type = PropertyInfo.STRING_CLASS;
                arg2.name = "last_name";
                break;
            case 2:
                arg2.type = PropertyInfo.STRING_CLASS;
                arg2.name = "address";
                break;
            case 3:
                arg2.type = PropertyInfo.OBJECT_CLASS;
                arg2.name = "city";
                break;
            case 4:
                arg2.type = PropertyInfo.OBJECT_CLASS;
                arg2.name = "state";
                break;  
            default:break;
        }
    }
    @Override
    public void setProperty(int arg0, Object arg1) {
        // TODO Auto-generated method stub
        switch(arg0)
        {
            case 0:
                firstname =  (String)arg1;
                break;
            case 1:
                lastname =  (String)arg1;
                break;
            case 2:
                address =  (String)arg1;           
                break;
            case 3:
                city = (String)arg1;
                break;
            case 4:
                state = (String)arg1;
                break;

            default:
                break;
        }
    }
}

在您撰写MainActivity的{​​{1}}中删除所有代码。

添加:

List<NameValuePair >Array = new ArrayList<NameValuePair>();

在doInBackground中添加:

Array array = new Array();
array.setFirstname(firstname);
array.setLastname(lastname);
array.setAddress(address);
array.setCity(city);
array.setState(state);

它会正常工作:)

答案 1 :(得分:0)

只需致电SoapObject#addProperty(key, value),如果您想要输入一系列键值对,请按以下方式修改代码:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("first_name", firstName);
request.addProperty("last_name", lastName);
request.addProperty("address", address);
request.addProperty("city", city);
request.addProperty("state", state);

答案 2 :(得分:0)

// Try this way,hope this will help you to solve your problem...

        HashMap<String,String> nameValuePairMap = new HashMap<String,String>();
        nameValuePairMap.put("first_name", firstName);
        nameValuePairMap.put("last_name", lastName);
        nameValuePairMap.put("address", address);
        nameValuePairMap.put("city", city);
        nameValuePairMap.put("state", state);



        protected String doInBackground(HashMap<String,String> ... params) {



            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            Iterator<String> iterator = nameValuePairMap.keySet().iterator();

            while (iterator.hasNext()){
                String key = iterator.next();
                request.addProperty(key, nameValuePairMap.get(key));
            }



            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.implicitTypes = true;

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);


                SoapObject result = (SoapObject) envelope.bodyIn;
                Log.e("Soap Object Output", String.valueOf(result));


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

                Log.e("Error in Catch", e.toString());

            }

            return null;

        }