1

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;

        }
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
Girish Arora
  • 966
  • 6
  • 7
  • possible duplicate of [Intent putExtra ArrayList](http://stackoverflow.com/questions/18050030/intent-putextra-arraylistnamevaluepair) – Manish Dubey Apr 03 '14 at 05:49
  • @ManishDubey I have to send name value pair list using soap web service not as an intent – Girish Arora Apr 03 '14 at 05:52
  • why dont you use propertyInfo class for sending and setting up your parameter value – Riddhi Shah Apr 03 '14 at 06:09
  • @RiddhiShah PropertyInfo pInfo =new PropertyInfo(); pInfo.setName("array"); pInfo.setValue(Array); I had already try this method, but getting the same error – Girish Arora Apr 03 '14 at 06:26
  • @RiddhiShah Please let me know how to do, I had already tried PropertyInfo method as above but i am not able to do. If you have any other way of doing the same, please let me know – Girish Arora Apr 03 '14 at 06:29

3 Answers3

0

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 :)

W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
0

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);
exloong
  • 441
  • 3
  • 6
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;

        }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • this will not work, You will get the exception as it mentioned in question, and i got the solution and i answered for the question – W I Z A R D May 19 '14 at 16:55