编程语言
首页 > 编程语言> > c# – 空指针异常使得使用kso​​ap传递数组很困难

c# – 空指针异常使得使用kso​​ap传递数组很困难

作者:互联网

我有这样定义的数组:

 ArrayList<String> items = new ArrayList<String>();

我现在填充了该数组,我希望通过Web服务将其发送到我的远程数据库.

这是我的Android代码中执行ksoap工作的方法

  public String[] call2()
     { 
     SoapPrimitive responsesdata = null; 

     SoapObject request = new SoapObject(namespace, method_NAME);      
     request.addProperty("names",items);

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
     SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request);
     HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
     androidHttpTransport.debug = true; 

        try 
        {

        androidHttpTransport.call(soap_ACTION, envelope);

        //responsesdata = (SoapPrimitive) envelope.getResponse(); 

        SoapObject result = (SoapObject)envelope.bodyIn; 

        if(result != null)
        { 
            System.out.println("SOAP response:\n\n" + result.getProperty(0).toString()); 

        }


        System.out.println(" --- response ---- " + responsesdata); 

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

        System.out.println( " ----" + responsesdata );
        String serviceResponse= responsesdata .toString(); 
        System.out.println(serviceResponse);

        String[] temp; 
        String delimiter = ","; 
        temp= serviceResponse.split(delimiter);

        return temp; 

 }

但是,当我运行我的代码时,我得到Null Pointer Exception,请参阅下面的logcat:

            11-10 15:22:50.496: WARN/System.err(295): java.lang.RuntimeException: Cannot serialize: []
            11-10 15:22:50.496: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:629)
            11-10 15:22:50.505: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:613)
            11-10 15:22:50.515: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
            11-10 15:22:50.515: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
            11-10 15:22:50.515: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
            11-10 15:22:50.515: WARN/System.err(295):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
            11-10 15:22:50.515: WARN/System.err(295):     at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
            11-10 15:22:50.525: WARN/System.err(295):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:74) 
            11-10 15:22:50.555: INFO/System.out(295):  ----null
            11-10 15:22:50.565: DEBUG/AndroidRuntime(295): Shutting down VM
            11-10 15:22:50.565: WARN/dalvikvm(295): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
            11-10 15:22:50.586: ERROR/AndroidRuntime(295): FATAL EXCEPTION: main
            11-10 15:22:50.586: ERROR/AndroidRuntime(295): java.lang.NullPointerException
            11-10 15:22:50.586: ERROR/AndroidRuntime(295):     at com.example.display.call2(display.java:200)
            11-10 15:22:50.586: ERROR/AndroidRuntime(295):     at com.example.display$3.onClick(display.java:145)
            11-10 15:22:50.586: ERROR/AndroidRuntime(295):     at android.view.View.performClick(View.java:2408)

我的网络服务代码:

  public class Service1 : System.Web.Services.WebService
  {
     [WebMethod]

      public String getnames(String[] values)
      {

        try
         {

            using (SqlConnection myConnection = new SqlConnection(@"Data S  ource=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {
                int count = 1;
                int rows = 0;

                myConnection.Open();
                foreach (string student in values)
                {
                    count++;
                    using (SqlCommand myCommand = new SqlCommand())
                    {
                        myCommand.Connection = myConnection;
                        myCommand.CommandText = "insert into record values(@pCount, @pStudent)";
                        SqlParameter param = myCommand.CreateParameter();
                        param.ParameterName = "@pCount";
                        param.Value = count;
                        myCommand.Parameters.Add(param);

                        param = myCommand.CreateParameter();
                        param.ParameterName = "@pSudent";
                        param.Value = student;

                        rows = myCommand.ExecuteNonQuery();
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return "an error occured";
        }


        return "success";
       }
    } 
 }

解决方法:

这是KSOAP2 for Android库的一个已知问题,目前它根本不支持数组.问题描述如下:

http://code.google.com/p/ksoap2-android/issues/detail?id=19

可在此处找到第三方补丁,解决方案和示例:

http://people.unica.it/bart/ksoap2-patch/

看看这些链接,你可以找到你的答案.

谢谢.

标签:c,android,sql-server-2008,web-services,ksoap
来源: https://codeday.me/bug/20190630/1336510.html