简体   繁体   中英

Send list name value pair using soap web service from my android app

I have to send NameValuePair list to server using Soap webservice from my android app. I am getting error 04-03 11:09:51.693: E/Error in Catch(14070): java.lang.RuntimeException: Cannot serialize . Please help me. Below is my Code

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;

        }

I gave bounty for this question, and finally i found solution. Solution is you can't use List<NameValuePair > for SOAP web services. Instead of that you can use kvmserialization . Below is the solution for your query.

First Thing you need to download library form this 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;
        }
    }
}

And in your MainActivity where you wrote List<NameValuePair >Array = new ArrayList<NameValuePair>(); remove all that code.

Add this:

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

In doInBackground add this:

PropertyInfo piIssueData = new PropertyInfo();
                piIssueData.setName("array");
                piIssueData.setValue(array);
                piIssueData.setType(array.getClass());

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty(piIssueData);

It will work fine :)

just call SoapObject#addProperty(key, value) ,if you wanna put serials of key-value pairs, modify your code as follows:

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);
// 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;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM