2013-01-11 19 views
0

我的web服務;爲什麼顯示錯誤無法序列化?

[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; 
    } 

和代碼在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; 
    } 

爲什麼顯示例外:java.lang.RuntimeException: Cannot serialize: [Ljava.lang.String;@4051d0a0

+0

安置自己的日誌。 –

回答

1

多個屬性不能傳遞整個數組..所以你必須使用seprator @@的字符串..和傳遞服務......和改變服務。

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

和更改服務代碼像這樣

[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; 
} 
+0

如果我的param值有字符','。它顯示錯誤。 – mum

+0

看到我編輯的答案.... –

+0

如果arr [0] =「le @@,12」;這是不正確的。 – mum

1

替換該行

request.addProperty("arr", arr); 

與此

request.addProperty("arr", arr[0]); 

你不能將整個array.you應該通過它的一個元素。

更新 你可以添加

request.addProperty("prop1", arr[0]); 
request.addProperty("prop2", arr[1]); 
request.addProperty("prop3", arr[2]); 
+0

如果發送一個元素,它會很長。哪種方式。發送數組? – mum

+0

arr是字符串array.and只寫arr意味着整個數組不是它的一個元素。 –

+0

如果Array的長度非常大,並且陣列的長度是動態的。你的方式不好。 – mum