空指针异常使得使用 ksoap 传递数组变得困难

发布于 2024-12-14 20:17:47 字数 5224 浏览 3 评论 0 原文

我的数组定义如下:

 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; 

 }

但是,当我运行代码时,我收到空指针异常,请参见下面的 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)

我的 Web 服务代码:

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

I have array defined like this :

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

I have populated that array now and I wish to send it via a web service to my remote database.

Here is the method in my Android code that does the ksoap work

  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; 

 }

However when I run my code I am getting Null Pointer Exception see in logcat below:

            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)

My web service code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2024-12-21 20:17:47

这是 KSOAP2 for Android 库 的一个已知问题,目前该库根本不支持数组。问题描述位于:

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

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

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

查看这些链接,您可以找到答案。

谢谢。

It is a known issue with the KSOAP2 for Android library, which at the moment simply does not support arrays. The issue description is here:

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

A third-party patch, solution and an example can be found here:

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

Look on these link, You can find your answer on that.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文