gpt4 book ai didi

c# - 空指针异常使得难以使用 ksoap 传递数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:07:43 26 4
gpt4 key购买 nike

我有这样定义的数组:

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

我现在已经填充了那个数组,我希望通过网络服务将它发送到我的远程数据库。

这是我的 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)

我的网络服务代码:

  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# - 空指针异常使得难以使用 ksoap 传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com