Search code examples
androidksoap2

Why show error Cannot serialize?


My webservice;

[WebMethod]
    public int insertNhanVien(string[] arr)
    {

        SqlConnection con = new SqlConnection();
        // con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Bai1;Integrated Security=True";
        con.ConnectionString = "server=.\\SQLEXPRESS;database=QLNV;uid=sa;pwd=123456";
        con.Open();
        int n = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            string[] s = arr[i].ToString().Split(',');

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert Into MUser(Ten,Tuoi) values(" + s[0].Replace("'", "''") + "," + s[1] + ")";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            n = cmd.ExecuteNonQuery();
        }
        return n;
    }

And code in android:

private boolean insertNhanVient() {
        boolean result = false;
        try {

            String NAMESPACE ="http://tempuri.org/";
            String METHOD_NAME ="insertNhanVien";
            String URL ="http://localhost:10829/WebSite/Service.asmx";
            String SOAP_ACTIONS = NAMESPACE + "/" + METHOD_NAME;
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            String [] arr =new String[3];
            arr[0]="le,12";
            arr[1]="hoang,33";
            arr[2]="nhung,23";
            request.addProperty("arr", arr);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

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

            try {
                androidhttpTranport.call(SOAP_ACTIONS, envelope);
            } catch (IOException e3) {              
                result = false;

            } catch (XmlPullParserException e3) {

                result = false;
            }
            Object responseBody = null;
            try {
                responseBody = envelope.getResponse();
                String t = responseBody.toString();
                if (t.equals("1")) {
                    result = true;
                }
            } catch (SoapFault e2) {

                result = false;
            }
        } catch (Exception e) {

            result = false;
        } finally {
        }
        return result;
    }

Why show exception: java.lang.RuntimeException: Cannot serialize: [Ljava.lang.String;@4051d0a0 ?


Solution

  • you can't pass whole array.. so you have to use seprator @@ in String ..and pass it service... and change on service respectivley.

    String commasepratedString="";
    for(int i=0;i<arr.length();i++)
    {
    if(i!=(arr.length-1)) 
    {
    commasepratedString=commasepratedString+arr[i]+"@@";
    }
    else
    {
    commasepratedString=commasepratedString+arr[i];
    }
    }
    
     request.addProperty("arr", commasepratedString);
    

    and change service code like this way

    [WebMethod]
    public int insertNhanVien(string commasepratedString)
    {
        String arr[] = commasepratedString.Split('@@');
        SqlConnection con = new SqlConnection();
        // con.ConnectionString = "Data Source=.\\SQLEXPRESS;InitialCatalog=Bai1;          Integrated Security=True";
        con.ConnectionString = "server=.\\SQLEXPRESS;database=QLNV;uid=sa;pwd=123456";
        con.Open();
        int n = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            string[] s = arr[i].ToString().Split(',');
    
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert Into MUser(Ten,Tuoi) values(" + s[0].Replace("'", "''") + "," + s[1] + ")";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
    
            n = cmd.ExecuteNonQuery();
        }
        return n;
    }